replace method in String

I have a String a ="Tom/grade"
I try to replace "/" by "\"

so I do:
a = a.replaceAll("/", "\\");

but after this line, I can not see a's value by break point, anything wrong? thanks
keepworkingAsked:
Who is Participating?
 
sciuriwareCommented:
Why "\\\\" ?
Because it makes 2 jumps:  "\\\\" is a way to store "\\"   which means \ escaped to really effect a single \

Confusing, I know.

;JOOP!
0
 
Curtis ShullFounderCommented:
You can try:

a = a.replaceAll("//", "\\");


Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

You could try this:       String a = "Tom/grade";
                                 a = a.replaceAll(""Tom/grade", "Tom\grade");

A common error is for developers to expect replaceAll to literally replace all occurrances one string with another, but the replaceALL method isn't for that!
replaceAll() uses a regular expression to determine what to replace. If the string you're giving it as the pattern to replace doesn't compile to what you think it does it will give unexpected results.

Do you really need the replaceALL method??
If you're simply trying to replace one character like that, use the replace  method  from a given starting index say "4":



      if a is your string,

a = a.substring(4).replaceFirst("//", "\\");


If that doesn't fit your needs and you DO need the  replaceALL method then remember that characters '(' and ')' are treated as special characters by regex so one should use '"\\(" in the left part of the replaceAll for correct operation

Hope this helps!!.
0
 
keepworkingAuthor Commented:
thanks

I am trying your solution
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
keepworkingAuthor Commented:
a = a.replaceAll("//", "\\");
doesn't work
0
 
sciuriwareCommented:
Wrong, should be:   a = a.replaceAll("/", "\\\\");

;JOOP!
0
 
sonthoshCommented:
Try this...
a = a.replace('/', '\\');
0
 
sciuriwareCommented:
sonthosh, keepworking asked why his code didn't work. That was the question.

Of course there are other solutions, like:

int i = a.indexOf('/');
a = a.substring(0, i) + '\\' + a.substring(i + 1);

but that doesn't answer the question.

;JOOP!
0
 
JoeCommented:
This will work for you using the replace all:

        String test = "need / / / these replaced";
       
        test = test.replaceAll("\\/", "\\\\");
       
       
        System.out.println(test);



Joe
0
 
sciuriwareCommented:
JoeZ430, there is already a working solution present.
Why did you add this?

;JOOP!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> after this line, I can not see a's value by break point, anything wrong?

If the problem is just that you cannot see it in the debugger or in a watch (assuming you are using some IDE like Eclipse or Netbeans), it might be just because the control stepped out of the scope of 'a'
0
 
keepworkingAuthor Commented:
thanks everyone for the reply:

a = a.replace('/', '\\');                                                                                 //doesn't work since I don't knwo how many / in other Strings

                              to mayankeagle
:it might be just because the control stepped out of the scope of 'a'                      //No.

                              sciuriware :
a = a.replaceAll("/", "\\\\");                                                                               //works, but not a = a.replaceAll("/", "\\");    why??????

                             JoeZ430 :  
test = test.replaceAll("\\/", "\\\\");                                                                      //works but why not a = a.replaceAll("/", "\\");   ??????

 Onyx23 : I don't understand you, would you like to make your explainatioion easier?


thanks all!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> it might be just because the control stepped out of the scope of 'a'                      //No.

Then explain what you meant by you can't see that value in the debugger.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.