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?
split() The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings.
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])
.
String st = "abcd1234";
String st1=st.replaceAll("[^A-Za-z]", "");
String st2=st.replaceAll("[^0-9]", "");
System.out.println("String b = "+st1);
System.out.println("Int c = "+st2);
Output
String b = abcd
Int c = 1234
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With