I have two types of Strings. One is "abcdEfgh"
and "abcd efgh"
. That means first String is having upper case letter in between and second string is having white space. So now how do I check these two pattern string in java and make two strings.
String givenString;
if (givenString.equals("abcdEfgh")) {
String str1 = abcd;
String str2 = Efgh;
} else (givenString.equals("abcd efgh") {
String str1 = abcd;
String str2 = efgh;
}
Please provide the solution Thanks
You can split using regex \\s|(?=[A-Z])
\\s
is to deal with case of whitespace.(?=[A-Z])
is positive lookahead. It finds capital letter but keeps the delimiter while splitting..
String givenString;
String split[] = givenString.split("\\s|(?=[A-Z])");
String str1 = split[0];
String str2 = split[1];
for both cases
Test case 1
//case 1
givenString = "abcdEfgh";
str1 = abcd
str2 = Efgh
Test case 2
//case 2
givenString = "abcd efgh";
str1 = abcd
str2 = efgh
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