I suppose from the fact that you only mention 1, 2, 3 and 4 as substrings that you only want to retrieve one-digit substrings. To do so, you first have to split on whitespace:
String[] split = str.split(' ')
Then you can retain all elements of the array with length 1:
List<String> result = new ArrayList<>();
for (String elem : split) {
if (elem.length() == 1) {
result.add(elem);
}
}
Note that you can parse strings to integers using Integer.parseInt(String str)
.
A different approach would be to extract one-digit numbers with a regex: [^\s]([0-9])[$\s]
Edit: Based on your given String, also 9 and 6 would land in the result
List, as they are also one-digit numbers