aspose file tools
The moose likes Java in General and the fly likes replace value of string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "replace value of string" Watch "replace value of string" New topic
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
    
    4
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
    
    4
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
    
  19


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)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: replace value of string
 
Similar Threads
String index out of range: -19
replaceAll
How to replace special characters
string question
mock question