?
Solved

replace method in String

Posted on 2006-05-17
12
Medium Priority
?
196 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:keepworking
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 3

Assisted Solution

by:Onyx23
Onyx23 earned 100 total points
ID: 16704789
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
 

Author Comment

by:keepworking
ID: 16704896
thanks

I am trying your solution
0
 

Author Comment

by:keepworking
ID: 16704911
a = a.replaceAll("//", "\\");
doesn't work
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 700 total points
ID: 16706167
Wrong, should be:   a = a.replaceAll("/", "\\\\");

;JOOP!
0
 

Expert Comment

by:sonthosh
ID: 16706934
Try this...
a = a.replace('/', '\\');
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 700 total points
ID: 16706956
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
 
LVL 16

Assisted Solution

by:Joe
Joe earned 200 total points
ID: 16707283
This will work for you using the replace all:

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



Joe
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16707401
JoeZ430, there is already a working solution present.
Why did you add this?

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16708121
>> 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
 

Author Comment

by:keepworking
ID: 16712011
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
 
LVL 24

Accepted Solution

by:
sciuriware earned 700 total points
ID: 16715170
Why "\\\\" ?
Because it makes 2 jumps:  "\\\\" is a way to store "\\"   which means \ escaped to really effect a single \

Confusing, I know.

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16716193
>> 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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question