473,408 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Replace all "\\" substring with "\\\\"

Hi all,

String x = "\\";

This string will have the value \.
Now for any string contains \, i need to replace with \\.

But when i use x = x.replaceAll("\\", "\\\\");
I am getting runtime exception.

How can i replace this.
Dec 12 '07 #1
2 2331
JosAH
11,448 Expert 8TB
Hi all,

String x = "\\";

This string will have the value \.
Now for any string contains \, i need to replace with \\.

But when i use x = x.replaceAll("\\", "\\\\");
I am getting runtime exception.

How can i replace this.
You have to double all the backslashes once more because a single backslash
is not only special to the Java compile (javac), it is also special to the regular
expression compiler and interpreter.

kind regards,

Jos
Dec 12 '07 #2
BigDaddyLH
1,216 Expert 1GB
What Jos writes is true, but let me suggest a simpler solution.

String has two methods:
Expand|Select|Wrap|Line Numbers
  1. String replaceAll(String regex, String replacement)
  2. String replace(CharSequence target, CharSequence replacement)
  3.  
(CharSequence is an interface implemented by String.) Both methods replace "all occurrences", but method replaceAll is more powerful because it takes a pattern string. However, in your case that just gets in the way because backslash has a special meaning in patterns. So prefer to use the second method unless you really need to use a regular expression pattern:
Expand|Select|Wrap|Line Numbers
  1. x = x.replace("\\", "\\\\");
  2.  
Read more about regular expressions here:

http://java.sun.com/javase/6/docs/ap...x/Pattern.html
Dec 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
3
by: David Greenberg | last post by:
Hi I have a character field, varchar(60), and I want to replace only a part of it, a set substring which is always in the same set positions. Does anyone have an Sql statement or algorithm/sp ...
4
by: SM | last post by:
Hello, I have a simple question, but can't find the answer. I have a string that contains a path to a file I want to add another string to the end of that string So, if i have : path =...
7
by: silverburgh.meryl | last post by:
Hi, If I have a string like this: char buff; buff ='h'; buff ='e'; buff ='l'; buff ='l'; buff ='o';
4
mikewagan
by: mikewagan | last post by:
Hello guys, need help here on C! I need a function to replace a substring inside a string with another new string. Example, we have "you are famous than me", we need to replace "famous" with "more...
3
by: nityagopikrishnan | last post by:
how do i replace a substring.....eg hello world ...replace hello with hai an d you get hai world
13
by: abcmavdi | last post by:
i want to replace a word in given string e. g string ---- He is a good man. in above string i want to replace "good" word with "gentle" so finally i want to print bellow string :-
2
by: RJSF | last post by:
Hi, can someone please help me from my pit of despair.... I have a table - "Drawing" with a column "Contract" -varchar, which contains a 19 character string. E.g. 002559-002677-AN-01 or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.