• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to replace colon (:) in a String

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Team,

I have a requirement like replace the colon ( in a string.
I tried with the following code:
/**
String str = "abc ef:ghi";
str.replaceAll(":","_");
system.out.println(str);
**/
But the output i am getting the string with unchanged.

output
----------
abc ef:ghi

Could you please let me know why i am not able to replace the colon?
Also point me to the correct code that satisfy my requirement.

Thanks.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

You wrote:
-------------------------------------------------------------

Hi Team,

I have a requirement like replace the colon ( in a string.
I tried with the following code:
/**
String str = "abc ef:ghi";
str.replaceAll(":","_");
system.out.println(str);
**/
But the output i am getting the string with unchanged.

output
----------
abc ef:ghi

Could you please let me know why i am not able to replace the colon?
Also point me to the correct code that satisfy my requirement.

Thanks.

---------------------------------------------------------------

the replaceAll method will return a String which is not assigned to any variable. Because of this you are not seeing the change. Replace your code

str.replaceAll(":","_");

with

str = str.replaceAll(":","_");

This will work.
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kool Dada",

Please check your private messages for an important message from me.

Kind regards,
Katrina Owen
Saloon Bartender
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic