Kranthi
Kranthi

Reputation: 1

String Array Split

String s = "HELLO Java, JAVA IS INDEPENDENT. jAvA";

Here we can change all word JAVA to upper case by splitting it.

    String[] s2 = s.split(" ");

    for (String v : s2) {
        System.out.println(v);
    }

Again how do we join them after correcting the case?

Upvotes: 0

Views: 67

Answers (3)

Mohan Raj
Mohan Raj

Reputation: 1132

     String s = "HELLO Java, JAVA IS INDEPENDENT. jAvA";         
     Pattern p=Pattern.compile("java.*", Pattern.CASE_INSENSITIVE);
     String[] s2=s.split("\\s");
      s="";
     for (String v : s2) {
         Matcher m=p.matcher(v);             
         if(m.matches()==true){
             v=v.toUpperCase();
             s=s+" "+v; 
         }
         else
            s=s+" "+v;                      
     }
     System.out.println(s);

Upvotes: 0

MLavrentyev
MLavrentyev

Reputation: 1969

You can just concatenate the string back together like so:

String finalStringWeNeed = "";
for(String v : s2) {
    finalStringWeNeed = finalStringWeNeed + v + " ";
}

Upvotes: 0

yngwietiger
yngwietiger

Reputation: 1184

Just use a regex, like this:

String x = s.replaceAll("[j|J][a|A][v|V][a|A]", "JAVA");

That's easier than using the split, especially since one of your Strings would have a comma in it (i.e. "Java,").

Upvotes: 3

Related Questions