tl;dr
The modern solution uses Unicode code point numbers rather than the outmoded char type.
Here is an IntStream, a successive stream of each character’s code point number, printing each of those numbers to console:
"S1234567I"
.codePoints()
.forEach( System.out :: println )
83
49
50
51
52
53
54
55
73
Show each character along with its code point number. To convert a code point number back into a character, call Character.toString while passing the integer: Character.toString( codePoint ).
String s = Character.toString( 49 ) ; // Returns "1".
…and…
String s = Character.toString( 128_567 ) ; // Returns "" FACE WITH MEDICAL MASK.
Example:
"S1234567I".codePoints().forEach( ( int codePoint ) -> {
String message = Character.toString( codePoint ) + " → " + codePoint;
System.out.println( message );
} );
S → 83
1 → 49
2 → 50
3 → 51
4 → 52
5 → 53
6 → 54
7 → 55
I → 73
Unicode code point
The char type is obsolete, unable to represent even half of the 143,859 characters defined in Unicode. The char type is a 16-bit number underneath, capable of representing a range of numbers of about ± 64,000. Unicode characters are assigned numbers along a range of about a million, too big for char to handle.
Instead, use Unicode code point integer numbers to represent individual characters.
We can get a stream of int primitive values (IntStream) from a string, each number representing the Unicode code point of each successive character.
IntStream intStream = "S1234567I".codePoints() ;
Process each code point number. Here we simply print each number.
intStream.forEach( System.out :: println );
When run.
83
49
50
51
52
53
54
55
73
Or perhaps you want an array of the int numbers.
int[] codePoints = "S1234567I".codePoints().toArray();
Dump to console.
System.out.println( "codePoints = " + Arrays.toString( codePoints ) );
codePoints = [83, 49, 50, 51, 52, 53, 54, 55, 73]
Or perhaps you want a List object containing all those code point numbers. Here is a non modifiable list made by List.of. We call boxed to invoke auto-boxing feature to convert int primitives into Integer objects. Then a Collector implementation gathers the output of the stream into a List.
List < Integer > codePoints = "S1234567I".codePoints().boxed().collect( Collectors.toList() );
Explaining those parts:
List < Integer > codePoints = // Desired result is a `List` collection of `Integer` objects.
"S1234567I" // Your input string.
.codePoints() // Generate an `IntStream`, a succession of `int` integer numbers representing the Unicode code point number of each character in the `String` object.
.boxed() // Convert each `int` primitive to an `Integer` object.
.collect( // Collect the produced `Integer` objects together.
Collectors.toList() // Specify a `Collector` implementation that knows how to make a `List` object, containing our `Integer` objects.
) // Returns a `List` of `Integer` objects.
;
Pull digits
Perhaps you want to filter out the alphabetic characters to leave only the digits found in your input string. The Character class offers tests such as isDigit.
For an input of "S1234567I", that means dropping the S and the I, leaving 1234567, producing the integer number 1,234,567.
List < Integer > codePointsOfDigitsFromInput = "S1234567I".codePoints().filter( ( int codePoint ) -> Character.isDigit( codePoint ) ).boxed().collect( Collectors.toList() );
Break that out to multiple lines.
List < Integer > codePointsOfDigitsFromInput =
"S1234567I"
.codePoints()
.filter(
( int codePoint ) -> Character.isDigit( codePoint )
)
.boxed()
.collect( Collectors.toList() );
codePointsOfDigitsFromInput = [49, 50, 51, 52, 53, 54, 55]
We can modify that code to generate a String containing only digits taken from that input. See Question, Make a string from an IntStream of code point numbers?. And then we make an int integer number of that text.
String numberComponentFromInput =
"S1234567I"
.codePoints()
.filter(
( int codePoint ) -> Character.isDigit( codePoint )
)
.collect( // Collect the results of processing each code point.
StringBuilder :: new , // Supplier<R> supplier
StringBuilder :: appendCodePoint , // ObjIntConsumer<R> accumulator
StringBuilder :: append // BiConsumer<R,R> combiner
)
.toString();
int x = Integer.valueOf( numberComponentFromInput );
numberComponentFromInput = 1234567
x = 1234567