• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Splitting a string with “\”

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
javamate06
Asked:
javamate06
  • 4
1 Solution
 
riaancorneliusCommented:
string tmp = C:\e\a\f\e.rr;
tmp = tmp.replaceAll("\\", "\\\\");
0
 
riaancorneliusCommented:
\ 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
 
javamate06Author Commented:
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
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.

 
riaancorneliusCommented:
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
 
riaancorneliusCommented:
and then when you split it you also use "\\".
0
 
StillUnAwareCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now