• Post Reply Bookmark Topic Watch Topic
  • New Topic

replace value of string  RSS feed

 
manishraj kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
You can't replace part of a String, only replacing the String with a different String.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!