wood burning stoves 2.0*
The moose likes Java in General and the fly likes Replace in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Replace in a string" Watch "Replace in a string" New topic
Author

Replace in a string

Sumit Aggarwal
Greenhorn

Joined: Jul 31, 2011
Posts: 11
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

Joined: Aug 16, 2005
Posts: 14195
    
  20

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Sumit Aggarwal
Greenhorn

Joined: Jul 31, 2011
Posts: 11



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

Joined: Aug 06, 2009
Posts: 146

Hi Rowen,


As Jesper suggested use following code


Sumit Aggarwal
Greenhorn

Joined: Jul 31, 2011
Posts: 11
Thanks and how do I escape a '\' backslash





How do i achieve this.
Input String : 11\20
Output String : 11\\20
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Modify the input String to
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
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

Joined: Mar 17, 2011
Posts: 7894
    
  21

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Mar 17, 2011
Posts: 7894
    
  21

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

Joined: Oct 13, 2005
Posts: 39095
    
  23
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.
 
GeeCON Prague 2014
 
subject: Replace in a string