Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

change a string

 
david lightman
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
think I've got it. thanks.
[ April 11, 2005: Message edited by: david lightman ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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>
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic