Replace the ' by /'

  • A+
Category:Languages

I'm trying to replace a single quote ' with this character /', the final goal is to transform this chay'ekka to chay/'ekka. I tried this code:

String cityName = new String("chay'ekka"); System.out.println("before : " + cityName); cityName = cityName.replaceAll("'", "//'"); System.out.println("after : " + cityName); 

but the result is still chay'ekka.

Are there any other methods or functions in java to achieve this goal?


You're using replaceAll, which works with regular expressions. While you can fix that by doubling the backslashes, there's no indication that you want regular expressions at all. It would be simpler just to call String.replace instead, as that doesn't use regular expressions:

String cityName = new String("chay'ekka"); System.out.println("before : " + cityName); cityName = cityName.replace("'", "//'"); System.out.println("after : " + cityName); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: