Solved

Splitting a string with “\”

Posted on 2006-07-16
6
232 Views
Last Modified: 2010-03-31
Hi,
I want to split this string

C:\e\a\f\e.rr

With splitter (“\”) but it is not accepting because it expects “\n” or “\t”?

I want to split to double the “\”. So, I want to replace each

\

By

\\

So, this string
C:\e\a\f\e.rr

Must be
C:\\e\a\\f\\e.rr
0
Comment
Question by:javamate06
  • 4
6 Comments
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17119850
string tmp = C:\e\a\f\e.rr;
tmp = tmp.replaceAll("\\", "\\\\");
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17119851
\ is an escape character, so in a java string you need to use \\ instead of \. Rule of thumb is to just double the amount of backslashes you want.
0
 

Author Comment

by:javamate06
ID: 17119885
Thank you

Do you mean \ is equal to \\
?

because I tried

string tmp = C:\e\a\f\e.rr;
tmp = tmp.replaceAll("\\", "\\\\");

but it does NOT work.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
riaancornelius earned 25 total points
ID: 17119907
my mistake...

Where does the string "C:\e\a\f\e.rr" come from? Do you specify it? Because if you do, you need to specify it as "C:\\e\\a\\f\\e.rr" and then if you print it out you'll see "C:\e\a\f\e.rr"
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17119911
and then when you split it you also use "\\".
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 17119914
Having this executed

    String tmp = "C:\\e\\a\\f\\e.rr";
    System.out.println(tmp); // tmp = C:\e\a\f\e.rr
    tmp = tmp.replaceAll("\\\\", "\\\\\\\\");
    System.out.println(tmp);

the result will be

    C:\e\a\f\e.rr
    C:\\e\\a\\f\\e.rr
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

713 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