Skip to content
Advertisement

How to split the string into string and integer in java?

I have the String a="abcd1234" and I want to split this into String b="abcd" and Int c=1234. This Split code should apply for all king of input like ab123456 and acff432 and so on. How to split this kind of Strings. Is it possible?

Advertisement

Answer

You could try to split on a regular expression like (?<=D)(?=d). Try this one:

String str = "abcd1234";
String[] part = str.split("(?<=\D)(?=\d)");
System.out.println(part[0]);
System.out.println(part[1]);

will output

abcd
1234

You might parse the digit String to Integer with Integer.parseInt(part[1]).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement