String text = "mama mula ramu"; String find = " mu\nl a"; String replace = "bila"; String result = text.replace(find, replace) 

How to make the result String result = "mama bila ramu"?

  • space must also be deleted between l and a ? Or meant that spaces too all to hell? if there are spaces too, what to do if you need to replace the phrase? or "hello to you neighbor" for something else? - Alexey Shimansky

3 answers 3

You can check at https://www.compilejava.net/
For some reason, an Внутренняя ошибка on ideone .

 public class Program { public static void main(String[] args) { String text = "mama m ula ramu"; String find = " mu\nl a"; String replace = "bila"; find = find.replaceAll("\\W", ""); find = find.replaceAll("(?!^)(.)", "\\\\s*$1"); System.out.println(find); String result = text.replaceAll(find, replace); System.out.println(result); } } 
  • Great, thank you. Only the result is here: System.out.println(find); - m \ s u \ s la, but I must assume that m \ s u \ s l \ s * a. You can fix it, otherwise I’m not at all friends with the regular season) - user200303
  • @ user200303, fixed. - Qwertiy

We use the regular list and delete these characters from the pre- find (both in pairs and free-standing). And spaces are also cut.

 String text = "mama mula ramu"; String find = " mu\nl a"; String replace = "bila"; find = find.replaceAll("(\\r|\\n|\\r\\n)+", "").replaceAll("\\s+",""); // find = find.replaceAll("(\\r|\\n|\\r\\n|\\s)+", ""); String result = text.replace(find, replace); System.out.println(result); 

If you leave spaces, then the part with .replaceAll("\\s+","") (or \\s from the commented out expression) is thrown away.

  • Thanks It works. And how can you make it change if there are also invisible characters in the text? For example, String text = "mama mu la ramu"; or String text = "mama mul\na ramu"; - user200303
  • @ user200303 for me personally, such data is generally not enough. A more detailed and detailed description is needed, such as 1) what is meant by "invisible characters" 2) a complete list of the replacement rules in the source text. For example, from mama mu la ramu into what should turn into mama bila ramu or mama bi la ramu or something. What is the replacement logic in general? And if there are more words in the source text, and not this particular template, and if there are more words in the replaced text, then how they should coincide and determine what exactly this word needs to be replaced ... without details, for me it is a guessing on the coffee grounds - Alexey Shimansky
  • 1) Invisible characters - space, line break and tab. 2) It is necessary to replace with the exact string that is specified in the String replace; 3) And the words for replacement can be many in the text. String text = "mama mu la m ula mula ramu m/nula" , and the result will be mama bila bila bila ramu bila - user200303

You need to remove all unprintable characters from the string

 str.replaceAll("[\\u0000-\\u001F\\u007F\\u0080-\\u009F]", "");