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?
10 Answers
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]).
4 Comments
\D matches all non-digit characters, while \d matches all digit characters. ?<= is a positive lookbehind (so everything before the current position is asserted to be a non-digit character), ?= is a positive lookahead (so everything after the current position is asserted as a digit). Does this help?You can do the next:
- Split by a regex like
split("(?=\\d)(?<!\\d)") - You have an array of strings with that and you only have to parse it.
1 Comment
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() );
}
}
7 Comments
([A-Z]|[a-z])+ and letters could also have been clubbed together like [a-zA-Z]. First issue is a downright error. The second one just works better.| pipe before [0-9]+ making an OR between letters and numbers. You should withdraw this as a solution or fix it after testing properly.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
1 Comment
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);
Comments
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?
Comments
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);
}}