java - How do I replace the a portion of the string with special characters -


i replace portion of string "\\_\\_\\_\\_\\_"

(e.g. string str = "hello world") 

if str.replaceall("world", "\\_\\_\\_\\_\\_");

i don't see "\" character in replaced string, expect show me "hello \_\_\_\_\_"

you need:

str = str.replaceall("world", "\\\\_\\\\_\\\\_\\\\_\\\\_"); 

see it.

\ escape character in java strings. mean literal \ need escape \ \\.

\ escape char regex engine as-well. \\ in java string sent regex engine \ not treated literally instead used escape following character.

to pass \\ regex engine need have \\\\ in java string.


since replacing string (not pattern) string, there no need regex, can use replace method of string class as:

input = input.replace("world", "\\_\\_\\_\\_\\_"); 

see it.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -