• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to replace a "." with any other string?  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Usha

Try this
System.out.println("This is a file. this has words. and chars".replaceAll("\\.", "-"));

-Chiru
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Usha Pnatha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java™ Tutorial about regular expressions.
 
Usha Pnatha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Campbell Ritchie, for the resource.
It's very useful & I managed to solve my problems.

Thank you.
Usha
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, certainly.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!