| Author |
How to replace a "." with any other string?
|
Usha Pnatha
Greenhorn
Joined: Aug 01, 2008
Posts: 27
|
|
Hello Ranchers,
I am using the String.replaceAll(String arg1,String arg2) method to replace some strings like {, . : ;} in a text with white spaces.
This works fine when I give {, : ;} as arg1.
But when I give a "." for arg1, it replaces the whole string with the string given for arg2.
i.e:
System.out.println("This is a file. this has words. and chars".replaceAll(".", "-")); ====> prints as "-----------------------------------------"
and
System.out.println("This is a file. this has words. and chars".replaceAll(".", " ")); ====> prints with " " (only white spaces of length same as the text)
How to solve this problem?
I want to replace all "." in my text with " "(white spaces).
Can anyone please help me in this regard?
Thanks in advance,
Usha
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
Hi Usha
Try this
System.out.println("This is a file. this has words. and chars".replaceAll("\\.", "-"));
-Chiru
|
-Chiru
|
 |
Jay Shukla
Ranch Hand
Joined: Jun 08, 2008
Posts: 214
|
|
Hi Chiru,
The solution worked. but i wondered why there are two backword slashed if escape character is required , i think only one slash is enough.
Could you please explain me.
Thanks.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
Jay Shukla wrote:
The solution worked. but i wondered why there are two backword slashed if escape character is required , i think only one slash is enough.
Could you please explain me.
Basically, the first parameter is not just a regular Java String, it is also a regex -- which is why you need to escape the dot, as it has special meaning to the regex. So, you need "\.". But....
The backslash has special meaning to the Java String. You need to pass an actual backslash to the regex engine, so you need to escape it, for the String itself (note: this wouldn't be necessary if it wasn't a string literal -- say if you had read the regex from a file.)
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
@ Shukla
replaceAll will take arg1 has RE, if we give . means all
so to escape that \. means not an valid escape sequence
\\. escape the escape sequence.
i think you got the stuff
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
One slash is enough to escape the dot in the regular expression, yes. But you must escape that slash again to escape the slash in a Java string.
You could also try String.replace(CharSequence, CharSequence); it has been added in Java 5.0 to basically be similar to String.replaceAll(String, String) but without using regular expressions.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Usha Pnatha
Greenhorn
Joined: Aug 01, 2008
Posts: 27
|
|
Yeah, it's working!!!
Thank you very much for all of you.
Can anyone please help me with replacing { "(", ")", "?" , "!" } ?
If there's any resource to refer, please let me know that also.
Thank you.
Usha
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
Java™ Tutorial about regular expressions.
|
 |
Usha Pnatha
Greenhorn
Joined: Aug 01, 2008
Posts: 27
|
|
Thank you very much Campbell Ritchie, for the resource.
It's very useful & I managed to solve my problems.
Thank you.
Usha
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
Usha Pnatha wrote:Thank you very much Campbell Ritchie, for the resource.
It's very useful & I managed to solve my problems.
Thank you.
Usha
You're welcome It is a good resource, isn't it. I trust you have bookmarked the tutorials.
|
 |
Usha Pnatha
Greenhorn
Joined: Aug 01, 2008
Posts: 27
|
|
Yeah, certainly.
|
 |
 |
|
|
subject: How to replace a "." with any other string?
|
|
|