I want to replace text in a variable.

The text itself is taken by JSON and there were extra spaces and html characters.

It seems to have gotten rid of them with this string (this is a string variable),

s.replaceAll("\\<.*?>", "").replaceAll("&nbsp;", "").trim(); 

but there is such a block of characters

{youtube} different character set {/ youtube}

Which team can replace the character set starting with "{youtube" and ending with "youtube}"?

(The content between these blocks always changes, so I cannot change it, but I would like to cut it off)

    3 answers 3

    So:

     "{youtube}разный набор символов{/youtube}".replaceAll("\\{youtube\\}[^\\{]*\\{\\/youtube\\}","выпилено"); 
    • Can you explain how the characters in the string work? when I add a string, it for some reason returns void, although this YouTube block at the end of the text is m.content = item.optString("content").replaceAll("\\<.*?>","").replaceAll("&nbsp;", "").replaceAll("\\{youtube\\}[^\\{]*\\{\\tube}","выпилено").trim(); - zayn1991
    • Corrected shielding the last brace. And you incorrectly copied the regulars. - Yura Ivanov
    • Now it helped =) - zayn1991

    So try:

     String text = "132 {youtube}раолов{/youtube} my ggfd 543 {youtube}мволов{/youtube} jhghjg"; int start = text.indexOf("{youtube}"); int end = text.indexOf("{/youtube}"); String subString = text.substring(start, end + 10); text = text.replace(subString, ""); 

      Something like this.

       String myTxt = "ваш текст"; StringBuilder myStringBuilder = new StringBuilder(myTxt); myStringBuilder.replace(myTxt.indexOf("{youtube}")+9, myTxt.indexOf("{/youtube}"),"нужный текст"); myTxt = myStringBuilder.toString(); 

      +9 is the size of the String "{youtube}". Experiment with what exact result you need - so that {youtube} itself is replaced or not?