This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String replace" Watch "String replace" New topic
Author

String replace

Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
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?


Rob Petterson
SCJP
krishnaraja Puranika
Greenhorn

Joined: Feb 06, 2003
Posts: 5
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

Joined: Jan 23, 2002
Posts: 149
Thanks Krishnaraja
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Feb 06, 2003
Posts: 5
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 ..........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String replace
 
Similar Threads
string question
Replace property in String
replace value of string
replaceAll
How to replace special characters