• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replace in a string

 
Sumit Aggarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to replace a string 11:20 with 11\:20, But when ever i try replacing the occurrences of ':' with '\:' it try escaping the '\' itself when used with "".
Kindly let me know how to achieve it. or in case you need more clarity.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code, then it's easier for other people to tell you exactly how to fix the problem.

Working with \ in Java source code can be a bit tricky, especially when dealing with regular expressions, because you have to escape the \ on multiple levels. Sometimes you need to write \\ or even \\\\ for a backslash.
 
Sumit Aggarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Lets suppose this is the code, what should I write in the replace(..,..) func that should fetch me this result?


Input String : 11:20
Output String : 11\:20


This is my requirement.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rowen,


As Jesper suggested use following code


 
Sumit Aggarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks and how do I escape a '\' backslash





How do i achieve this.
Input String : 11\20
Output String : 11\\20
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Modify the input String to
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is a way to convert "11\20" to "11\\20" by using replace method without modifying the original string. Tokenizing the string also may not help. I may be wrong. so we will wait for more replies
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to replace a string 11:20 with 11\:20, But when ever i try replacing the occurrences of ':' with '\:' it try escaping the '\' itself when used with "".

I don't think you're right there, but "\"s are a royal pain in the rear in Java.

Not only are they reduced by the String class (why, I have never understood), they are also interpreted by the regex compiler (which takes Strings).
Furthermore, the String class isn't consistent about its replace() methods: replace() takes a simple String, but replaceAll() and replaceFirst() take regexes.

However, the simple rule is this: Any time you want a '\' in a String you must put at least 2 of them. If the String is also a regex you have to put 4 (ugh!).

However, in your case replace(":", "\\:") should do the trick.

Winston
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Not only are they reduced by the String class (why, I have never understood)

\ is an escape character in many programming languages. Its need comes from the requirement to add certain characters in strings / chars. Without \, how would you create a String that contains ", or the character '? Now it's "\"" and '\''. It also gives you string and char literals for characters like tab (\t) and line break (\n).

Or would you prefer Visual Basic's way:
I know which one I prefer, and it's definitely not VB.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:\ is an escape character in many programming languages...

Sorry, I wasn't clear. I know what it's used for, I just don't know why they chose '\'; especially when it's already used by regex (not to mention Windows).

One of the other languages I use had the foresight to choose a different escape character for its Strings (in its case, '~'), which makes everything so much easier .

Winston
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason it was chosen is probably hidden in the mists of history, but C and C++ use similar escape sequences. Maybe it was because \ is unusual in ordinary writing. Eiffel uses %n or similar for escapes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic