Thursday 15 May 2014

java - Replace everything after last forward slash -



java - Replace everything after last forward slash -

i attempting remove after lastly forwards slash in url. wrote regex works in online java regex testers, unfortunately not in local code. insight in why may appreciated. right regex pattern, target string, , desired output follows.

regular expression: "[^/]*$" target string: /example/c/coffee101 desired output: /example/c/cafè999

the bare-bones java code:

target = "/example/c/coffee101"; replace = "cafè999"; target.replacefirst("[^/]*$", replace);

thank greatly!

you need assign homecoming value of replacefirst method target no method in string class changes string object itself:

target = target.replacefirst("[^/]*$", replace);

java regex replace

No comments:

Post a Comment