• 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

change a string

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a strings that comes in always as STRING1.CAT, STRING2.CAT., STRING3.CAT, but I want to convert each one to STRING1.DOG, STRING2.DOG, STRING3.DOG.

I know that each string will ALWAYS begin with 8 places(STRING#. then the last three.
so should i place this is a string buffer and and change cat to dog? how?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about using the replaceAll(String, String) method of the String class?
 
david lightman
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String dStr = cStr.replaceAll(cStr, "CAT", "DOG");


i see the method, but me being a newbie, how exactly would I execute?

thanks again
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pretty much as you have written it. You only have to worry if "CAT" can exist anywhere else in your string...
 
david lightman
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
think I've got it. thanks.
[ April 11, 2005: Message edited by: david lightman ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because my wife calls me "Captain Literal", i feel obligated to point something out.

You are not really changing the string here. cStr remains what it always was. What you are doing is createding a NEW string, based off the original, and creating a new reference to it.

you could do

cStr = cStr.replaceAll("CAT", "DOG");

but again, this creates a new string, and re-assigns the cStr referenct to point to the new one.

[edited to correct the call, per Ernest's note below]
[ April 11, 2005: Message edited by: fred rosenberger ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And because of my ongoing fight for truth, justice, and the American way, I need to point out that the first argument y'all keep sticking in there doesn't belong. It's just

cStr = cStr.replaceAll("CAT", "DOG");
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, i failed to completly read the posts. i plead guilty to just cut-n-pasting others errors, though i know that is a poor excuse.

<hangs head in shame>
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, since the input strings are always going to be in the same format, the following would probably be more efficient:



- Jeff
 
reply
    Bookmark Topic Watch Topic
  • New Topic