[Solved] Replace second repeated word in string [closed]


Split the string by space and rename the last word. Then using the StringBuilder concatenate them together back to your original String.

String str = "hello I am a example example"
String[] parts = str.split(" ");
parts[parts.length-1] = "moon";
System.out.println(parts[parts.length-1]); 
StringBuilder sb = new StringBuilder();
for (int i=0; i<parts.length; i++) {
   sb.append(parts[i]);
   sb.append(" ");
}
str = sb.toString();

5

solved Replace second repeated word in string [closed]