473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

36 New Member
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 2345
JosAH
11,448 Recognized Expert MVP
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 Recognized Expert Top Contributor
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
78777
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 find one.
3
34028
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 that could do that with one call or run ? Thanks David
4
2613
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 = document/disco/album/hello.doc i want it to become : document/disco/album/hello_large.xls
7
7952
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
2528
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 famous", so to get the output "you are more famous than me". How do we do that? Thanks for your help guys!
3
1507
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
8315
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
3363
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 002334-100385-AN-01 I need to change the 8th character and depending on what the original value is replace it with a different value, e.g...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.