各个字符与字符'0'的差就是对应的int值了
- package test;
- import java.util.Arrays;
- public class Test9 {
- public static void main(String[] args) {
- print(getInts("0123456asdfdf2243434"));
- }
- public static int[] getInts(String string) {
- char[] chars = string.toCharArray();
- int[] ints = new int[string.length()];
- int count = 0;
- for (int i = 0; i < chars.length; i++) {
- if (chars[i] >= '0' && chars[i] <= '9') {
- ints[count++] = chars[i] - '0';// 各个字符与字符'0'的差就是对应的int值了。
- }
- }
- return Arrays.copyOf(ints, count);
- }
- public static void print(int[] ints) {
- for (int i : ints) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- }
复制代码
|