I need to be able to break apart a string into its letters and its numbers. The strings it will be looking at are something along the lines of
"dir1" "path11"
I will also need to temporarily store the 2 values. I have found the .split method and it looks like it could be what I'm after but I can't figure out how to use it, should I keep looking into it or is there a better way?
Use String.split() as in
String[] strings = "abc111".split("(?=\\d+)(?<=\\D+)");
The split()
function takes a regex around which the string will be split up.
Here the pattern has two assertions: one which asserts that the characters ahead are digits alone and another one which asserts that the preceding characters are non-digits. So it will split just after the end of non-digits (which is just before the beginning of digits).
Regex and split can help to do that:
public static void main(String[] args) {
String[] c = "path11".split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
System.out.println(Arrays.toString(c));
}
output=
[path, 11]
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.