5

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

5
  • 1
    String class has a split method. givenString.split(" "); for the white space example. Commented Mar 3, 2017 at 9:29
  • 3
    And for the uppercase example, stackoverflow.com/questions/3752636/… Commented Mar 3, 2017 at 9:30
  • 2
    Possible duplicate of Java: Split string when an uppercase letter is found Commented Mar 3, 2017 at 9:32
  • 1
    it is not a duplicate, as the question is about both cases (capital or white space delimiter), not one of them. Commented Mar 3, 2017 at 9:38
  • Could it be mixed "abcdEfgh rest"? Are there always extactly two parts to find? Commented Mar 3, 2017 at 9:59

3 Answers 3

5

You can split using regex \\s|(?=[A-Z])

  1. \\s is to deal with case of whitespace.
  2. (?=[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
Sign up to request clarification or add additional context in comments.

2 Comments

@RamanSahasi Thanks for your ans. I am getting , str1=null and str2=abcd in the Test case 1
@PratibhaPatil I've compiled and tested this code. Please take a look at this online version of my test cases.
2

You need to combine the two conditions using the OR operator |. You've already figured out split by space is simply " ". The uppercase example is answered by Java: Split string when an uppercase letter is found

Example

String one = "abcdEfgh";
String two = "abcd efgh";

System.out.println(Arrays.toString(one.split(" |(?=\\p{Upper})")));
System.out.println(Arrays.toString(two.split(" |(?=\\p{Upper})")));

Output

[abcd, Efgh]
[abcd, efgh]

2 Comments

Thanks@Adam, both for upper case and white space same pattern ?
@PratibhaPatil Yep, as my example shows the same pattern can be used
0

Keep it simple, search for space in givenString instead of case sensitive matchs

if(givenString.indexOf(" ") != -1){
    System.out.println( "The string has spaces");
}else{
    System.out.println( "The string has NO spaces");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.