How can I divide words, work with each word? How can I add them to the array, change them, and then insert them back and print the contents of the line? The task is this: "in each word of the text, remove the subsequent occurrences of the first letter of this word."

StringBuffer text = new StringBuffer("Slava ochen krasivyi malchik"); 
  • The task to use is StringBuffer? - Sergey Gornostaev
  • In general, yes, but I would also like to see alternatives - Artem Aleksandrovich
  • 2
    Apparently the task is educational. For the first 2 questions, the String.split method will help you. Then write the word processing logic and collect it back - Victor
  • StringBuffer is deprecated in 2004. Use better StringBuilder. As for the task itself, in the given example each word contains only one copy of the letter with which it begins. - Sergey Gornostaev
  • 2
    @StasDorozhko before you an example of an error in the documentation. Getz in "Java Concurrency In Practice", Bloch in "Effective Java", Flanagan in "Java in a Nutshell" write that StringBuffer not only outdated, but absolutely inapplicable in a multi-threaded environment, and in single-threaded synchronization it makes it slow. - Sergey Gornostaev

1 answer 1

For example:

 String line = "Slava ochen krasivyi malchik"; StringBuffer stringBuffer = new StringBuffer(); for (String word : line.split(" ")){ stringBuffer.append(word.replace(word.substring(0, 1), "")); stringBuffer.append(" "); } System.out.println(stringBuffer.toString()); 

lava chen rasivyi alchik