Replacing \\ with \

Hi

I have a string

String st= "\\qwer\\d34x\\icxv"

I want to replace above string with the following

String st= "\wer\ete\x432 "

How can I do this.

Thx

Message was edited by:

Java_person

Message was edited by:

Java_person

[320 byte] By [Java_persona] at [2007-11-27 5:06:58]
# 1
Take a look at the String.replaceAll() method. Note that the replacement string is a regex not just a simple string.
jbisha at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...
# 2
Err.... what do you mean 'replace'? The lack of similarity of the two strings means you cannot really want to replace just the \\ with \ !
sabre150a at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...
# 3

> Err.... what do you mean 'replace'? The lack of

> similarity of the two strings means you cannot really

> want to replace just the \\ with \ !

Hmm, good point. I was going by the subject of the post when I replied; however, the actual post does not really make sense.

jbisha at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...
# 4

I have tried replaceAll(), but that doesnt gave me the result I wanted.

As far as meaning of replace is concerened we all know ENGLISH language and I dont think that I need to be philosophical to tell what is the meaning of "REPLACE".

The title of the post clearly shows what I want.

Thx

Message was edited by:

Java_person

Java_persona at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...
# 5

Come on, you posted that you wanted

String st= "\\qwer\\d34x\\icxv"

to be replaced with

String st= " \wer\ete\x432 "

So you also want to drop a q and other stuff in the string. Or do you want to change String st= "\\qwer\\d34x\\icxv" to String st= "\qwer\d34x\icxv" ?

If so , replaceAll is the way to do it.

PhHeina at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...
# 6

If you only want to replace \\ with \, and you're running JDK 1.5 or later, use replace() instead of replaceAll(): st = st.replace("\\\\", "\\");

The replaceAll() version would be st = st.replaceAll("\\\\\\\\", "\\\\");

uncle_alicea at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...
# 7

> I have tried replaceAll(), but that doesnt gave me

> the result I wanted.

Then you used it wrongly.

>

> As far as meaning of replace is concerened we all

> know ENGLISH language and I dont think that I need to

> be philosophical to tell what is the meaning of

> "REPLACE".

Based on the dissimilarity between the title and body of your post I think YOU need to know the meaning of 'REPLACE'.

> The title of the post clearly shows what I want.

Then, other than confusion, what does the body of your post add.

sabre150a at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...
# 8

> I have tried replaceAll(), but that doesnt gave me

> the result I wanted.

>

Then you did it wrong.

> As far as meaning of replace is concerened we all

> know ENGLISH language and I dont think that I need to

> be philosophical to tell what is the meaning of

> "REPLACE".

> The title of the post clearly shows what I want.

Don't get snitty. If you can't bother to communicate clearly and precisely, don't expect help.

jverda at 2007-7-12 10:25:54 > top of Java-index,Java Essentials,New To Java...