• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Replace in a string

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rowen,


As Jesper suggested use following code


 
Sumit Aggarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modify the input String to
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic