| Author |
replace value of string
|
manishraj kumar
Greenhorn
Joined: Mar 10, 2009
Posts: 2
|
|
|
i want to replace string value in java like this "i ammm am ammmamm".and only just replace am not ammm or ammmamm.how can i write code for replacing only these string value .
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32836
|
|
Welcome to JavaRanch
You can't replace part of a String, only replacing the String with a different String.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32836
|
|
|
Maneesh Godbole reminded me about the replace methods in the java.lang.String class; these might produce the result you require. They don't really replace anything, but create a new String with the alterations.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Assuming this is in reference to the (regex related) replaceXXX() methods of the java.lang.String class, you can actually specify a word border with the "\\b" tag.... so, if the pattern to be replaced is "\\bam\\b", this will only replace instance of "am" which are itself a word, and not attached to a word, hence, "amammmam" won't match.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: replace value of string
|
|
|