• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replacement backslash  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
How do you make this method work? I want to replace each backslash in a string to be replaced by a double backslash


 
Jonathan De Vlas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved it!

Me wrote:


Solution:



But I don't really understand why i needed "\\\\" , I thought I needed the escape character "\" + a normal backslash "\" to put a backslash in a string... weird
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan De Vlas wrote:
But I don't really understand why i needed "\\\\" , I thought I needed the escape character "\" + a normal backslash "\" to put a backslash in a string... weird


The backslash is an escape character for a regex replacement string -- so if you mean a literal backslash, then you need to escape it with another backslash. And as you mentioned, the backslash is an escape character for the java string literal too, so if you want to use a string literal, you need to escape the backslash, with another backslash, meaning two backslashes becomes four backslashes.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!