Reputation: 1099
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?
Upvotes: 16
Views: 100277
Reputation: 1
public class MyClass {
public static void main(String args[]) {
String a = "a1j2a3i4";
int i;
String str1="";
String str2="";
for(i = 0; i < a.length(); i++){
char c = a.charAt(i);
if( '0' <= c && c <= '9' )
str1=str1+c;
if( 'a' <= c && c <= 'z' )
str2=str2+c;
}
System.out.println(str1);
System.out.println(str2);
}}
Upvotes: 0
Reputation:
You can add some delimiter characters ⦀
to each group of symbols, and then split the string around those delimiters:
public static void main(String[] args) {
String[][] arr = {
split("abcd1234", "\u2980"),
split("ab123456", "\u2980"),
split("acff432", "\u2980")};
Arrays.stream(arr)
.map(Arrays::toString)
.forEach(System.out::println);
// [abcd, 1234]
// [ab, 123456]
// [acff, 432]
}
private static String[] split(String str, String delimiter) {
return str
// add delimiter characters
// to non-empty sequences
// of numeric characters
// and non-numeric characters
.replaceAll("(\\d+|\\D+)", "$1" + delimiter)
// split the string around
// delimiter characters
.split(delimiter, 0);
}
See also: How to split a string delimited on if substring can be casted as an int?
Upvotes: 0
Reputation: 67
try with this:
String input_string = "asdf1234";
String string_output=input_string.replaceAll("[^A-Za-z]", "");
int number_output=Integer.parseInt(input_string.replaceAll("[^0-9]", ""));
System.out.println("string_output = "+string_output);
System.out.println("number_output = "+number_output);
Upvotes: 0
Reputation: 2876
Use regex "[^A-Z0-9]+|(?<=[A-Z])(?=[0-9])|(?<=[0-9])(?=[A-Z])" to split the sting by alphabets and numbers.
for e.g.
String str = "ABC123DEF456";
Then the output by using this regex will be :
ABC
123
DEF
456
Upvotes: 0
Reputation: 17
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
Upvotes: 1
Reputation: 1479
public static void main(String... s) throws Exception {
Pattern VALID_PATTERN = Pattern.compile("([A-Za-z])+|[0-9]*");
List<String> chunks = new ArrayList<String>();
Matcher matcher = VALID_PATTERN.matcher("ab1458");
while (matcher.find()) {
chunks.add( matcher.group() );
}
}
Upvotes: 0
Reputation: 6718
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.
Upvotes: 3
Reputation: 5084
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])
.
Upvotes: 35
Reputation: 1830
You can do the next:
split("(?=\\d)(?<!\\d)")
Upvotes: 4
Reputation: 9601
A brute-force solution.
String a = "abcd1234";
int i;
for(i = 0; i < a.length(); i++){
char c = a.charAt(i);
if( '0' <= c && c <= '9' )
break;
}
String alphaPart = a.substring(0, i);
String numberPart = a.substring(i);
Upvotes: 0