java - replaceFirst does not work, but replace works on the exact same input? -


i writing program portion of needs replace part of string without deleting duplicates using replacefirst() not working properly.

input:

lock: "o_2_^-^" str:  " o_2_^-^ "  

code:

system.out.println(str); system.out.println(lock); system.out.println(str.contains(lock)); str = str.replacefirst(lock, ""); system.out.println(str); 

output:

 o_2_^-^  o_2_^-^ true  o_2_^-^  

the above real output program. though replace() method not work current situation did test , output compeletely different, in correct.

input:

lock: "o_2_^-^" str:  " o_2_^-^ "  

code:

system.out.println(str); system.out.println(lock); system.out.println(str.contains(lock)); str = str.replace(lock, ""); system.out.println(str); 

output:

 o_2_^-^  o_2_^-^ true   //empty line of output because string detected , removed. 

i have tried outside of writing own replacefirst() method, if has advice or input that'd great. thanks!

since replacefirst's 1st parameter assumed regex, need escape special characters. if not know beforehand lock going (example coming user-input), can use pattern.quote(lock) escape it.

see, also, short demo.


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -