Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java™ Tutorial about regular expressions.
 
Usha Pnatha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, certainly.


 
This will take every ounce of my mental strength! All for a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic