Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

String replaceAll problem

Thread view: 
Muhammad Ali - 13 Apr 2007 07:44 GMT
Hi,

I have string

String s = "Hello \"World\"";

when i print this on console, it prints

Hello "World"

It works fine. Now what i want to is to print

Hello \"World\"

Which means i have to replace the quotes (") with (\\) and then a
quote so that i may get a quote in output. so i used replaceAll method

System.out.println(s.replaceAll("\"", "\\\""));

But to my astonish, it prints out

Hello "World".

I wondered and then tried to do it again with

System.out.println(s.replaceAll("\"", "\\\\\""));

Now i was able to see the desired outout i.e; Hello \"World\"

But why i have to use 5 back slashes instead of 3 which were according
to the logic of "backsequences"?? I have seen the replaceAll method
and it is using the pattern matching in it.

If i try to replace the string using simple "replace" method it works
fine; i.e i don't have to use 5 backslashes, i use only 3 (according
to logic). Can someone explain why i have to use 5 slasheds in
replaceAll.
Philipp Leitner - 13 Apr 2007 08:46 GMT
Because '\' has a special meaning in a regex. Actually you have to
escape the '\' TWO TIMES when using it in a regex - first time to not
be interpreted as a special character in the Java String literal,
second time to not be interpreted as a special character in the
Regex :-)

I think I have read this somewhere in the Java API entry dealing with
Regexes some time ago. You should check there for more details.

/philipp

Btw.: this is one of the many reasons why I really /hate/ regex :-/
الصباغ - 14 Apr 2007 17:34 GMT
> Hi,
>
[quoted text clipped - 33 lines]
> to logic). Can someone explain why i have to use 5 slasheds in
> replaceAll.

now i using jbuilder7 and need newer version like what you using, can
you tell me how can i get it or if i can downlod from net.

thank you
Tris Orendorff - 14 Apr 2007 23:11 GMT
"=?utf-8?B?2KfZhNi12KjYp9i6?=" <sabd1983@yahoo.com> burped up
warm pablum in
news:1176568454.413829.298470@e65g2000hsc.googlegroups.com:

> now i using jbuilder7 and need newer version like what you using, can
> you tell me how can i get it or if i can downlod from net.

Why Soitainly!  Download free versions from:
http://www.codegear.com/Downloads/TrialandFreeVersions/JBuilder/
tabid/143/Default.aspx for non-commercial use.

Signature

Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.