Solved

Splitting a string with “\”

Posted on 2006-07-16
6
226 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 71
Non-recursive backtracking, using a stack 1 96
Java DateChooser? 3 29
servlet filter example 37 34
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now