There is, for example, a line:

"Описание серий, аннотации , изданных книг , Анонсы новых изданий, новости" 

How to replace the comma with the character " | ", and if there are spaces between words or phrases, remove them to get a line like this:

 "Описание серий|аннотации|изданных книг|Анонсы новых изданий|новости" 

    1 answer 1

    This can be easily done with regular expressions :

     String text = "Описание серий, аннотации , изданных книг , Анонсы новых изданий, новости"; String value = text.replaceAll("\\s*,\\s*", "|"); System.out.println(value); 

    Conclusion:

    Description of series | annotations | published books | Announcements of new editions | news

    In this code, I used the method String.replaceAll(regex, replacement) , where replacement is the string with which all substrings satisfying the regex expression will be replaced.

    Regular expression:

     \\s*,\\s* 

    means - all substrings containing from zero and more spaces before the comma \\s* and the same after it.

    It is also possible to replace regular expressions with objects of the java.util.regex.Pattern class and java.util.regex.Matcher objects.