Solved

Splitting a string with “\”

Posted on 2006-07-16
6
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

738 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