, i do not know how to correctly write this code.
My code:
String str = "23g32./'ef3";
int[] arr = new int[str.length()];
for (int digitsCount = 0; digitsCount < str.length(); digitsCount++)
for (int digitsArrayIndex = 0; digitsArrayIndex < str.length(); digitsArrayIndex++) {
if (Character.isDigit(str.charAt(digitsArrayIndex))) {
arr[digitsArrayIndex] = Integer.parseInt(String.valueOf(str.charAt(digitsArrayIndex)));
}
}
System.out.println("Array: " + Arrays.toString(arr));
}
My output:
Array: [2, 3, 0, 3, 2, 0, 0, 0, 0, 0, 3]
I want:
Array: [2, 3, 3, 2, 3]
A simpler way would be just to use one loop, and another index. When you have filled in the array, copy it using the value of the index
String str = "230g32./'ef3";
int index = 0;
int[] arr = new int[str.length()];
for (int digitsArrayIndex = 0; digitsArrayIndex < str.length(); digitsArrayIndex++) {
if (Character.isDigit(str.charAt(digitsArrayIndex))) {
arr[index++] = Integer.parseInt(String.valueOf(str.charAt(digitsArrayIndex)));
}
}
System.out.println("Array: " + Arrays.toString(Arrays.copyOf(arr, index)));
You can go more declarative and use Stream API for it:
final List<Character> filteredCharacters = str.chars()
.mapToObj(i -> (char) i)
.filter(Character::isDigit)
.collect(Collectors.toList());
One easy approach here would be to just do a regex replacement on the input string to strip away any non digits. Then, just use the character array from the resulting string.
String str = "23g32./'ef3";
char[] output = str.replaceAll("\\D+", "").toCharArray();
System.out.println(Arrays.toString(output)); // [2, 3, 3, 2, 3]
You can use Character.isDigit()
:
String str = "23g32./'ef3";
List<Integer> digits = new ArrayList<>();
for (char c : str.toCharArray()) {
if(Character.isDigit(c)){
digits.add(c - '0');
}
}
System.out.println(digits);
Output:
[2, 3, 3, 2, 3]
Never use obsolete char
, use only code point integers.
"23g32./'ef3".codePoints().filter( Character::isDigit ).map( Character::getNumericValue ).toArray()
See that code run live at IdeOne.com .
[2, 3, 3, 2, 3]
The char
type in Java is obsolete, unable to represent even half of the characters defined in Unicode. Instead, learn to use code point integer numbers.
The String#codePoints
method returns an IntStream
, that is, a stream of the code point integers, one for each character in your input string.
For each code point, ask if that character represents a digit as designated by the Unicode standard. If so, collect it. If not, ignore it.
Convert each of those digit characters into a number. Collect the numbers into your final array.
int[] digits =
"23g32./'ef3"
.codePoints()
.filter(
Character::isDigit
)
.map(
Character::getNumericValue
)
.toArray();
digits = [2, 3, 3, 2, 3]
Often a List
is more convenient than a mere array.
List < Integer > digits =
"23g32./'ef3"
.codePoints()
.filter(
codePoint -> Character.isDigit( codePoint ) // Or: Character::isDigit
)
.map(
codePoint -> Character.getNumericValue( codePoint ) // Or: Character::getNumericValue
)
.boxed()
.collect( Collectors.toList() ) // Or, in Java 16+, simply `.toList()`
;
See this code run live at IdeOne.com .
digits = [2, 3, 3, 2, 3]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.