Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replace

 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I need to replace values in a string with another value, but ignoring the first instance of it i.e. replace the exsisting value with the new value from index 1 onwards.
Can anyone tell me the method I should use?
 
krishnaraja Puranika
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can use the following code
String existing_string = "Hello";
String new_string = existing_string.subString(0,1) + "ow" ;
this results in a new string How. "ow" can be raplaced with any other desired string or string object.
bye
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Krishnaraja
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused by this one.

First, the question seems a little off... to replace values in a string with another value, but ignoring the first instance of it . But then the next bit contradicts this.. from index 1 onwards.

If you are replacing substrings, do you mean from index 1, or from wherever the first instance of your substring ends (all instances after the first)?

eg:

"abcdefgabcdefgabcdefg"

In this String, there are three instances of the substring "fg". First occurence is at index 5. To search "from" this point, and replace all other occurences of "fg" with "xx", you'd have to skip index 5 and 6, and start from 7.

"abcdefgabcdexxabcdexx"

All the supplied code does is saves the first letter of your original String and appends an entirely different String.

("axx" is what you'd get with my examples.)
 
krishnaraja Puranika
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...........what i thought was he just wants to replace the string with different letters except the first letter and the code also does so...
let me know what u need
thanx in advance ..........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic