Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

illegal escape character  RSS feed

 
Eugene Kanshin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I'm trying to replace all whitespaces in a string with underlines with:
String str;
str.replaceAll("\s+", "_");
but I've got illegal escape character error. Why it doesn't work?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eugene Kanshin:
Hello!
I'm trying to replace all whitespaces in a string with underlines with:
String str;
str.replaceAll("\s+", "_");
but I've got illegal escape character error. Why it doesn't work?


The escape symbol in Java is \. In order to pass the parameter correctly to the replaceAll method, you need to pass \s, so you'll need to escape the escape character.
 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use str.replaceAll("\\s+", "_");
 
Eugene Kanshin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. It works.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!