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

You can do the next:

  1. Split by a regex like split("(?=\\d)(?<!\\d)")
  2. You have an array of strings with that and you only have to parse it.

Use a regular expression:

Pattern p = Pattern.compile("([a-z]+)([0-9]+)");
Matcher m = p.matcher(string);
if (!m.find())
{ 
  // handle bad string
}
String s = m.group(1);
int i = Integer.parseInt(m.group(2));

I haven't compiled this, but you should get the idea.


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]).