Difference between revisions of "Replace every occurences of a string within a string"

From CodeCodex

(Java)
 
(Java)
Line 46: Line 46:
 
</highlightsyntax>
 
</highlightsyntax>
 
Keep in mind, that you need to escape characters like $ or | since they have special meaning when used in a regular expression.  
 
Keep in mind, that you need to escape characters like $ or | since they have special meaning when used in a regular expression.  
 +
 +
Since JDK 1.5, the String.replace() method can also be used. It does not take a regular expression so you don't need to worry about escaping special characters.
 +
<highlightsyntax language="java122">
 +
public class Test{
 +
  public static void main(String[] args){
 +
    String text = "hello world from www.rgagnon.com : hello world";
 +
 +
    System.out.println
 +
      (text.replace("hello world", "bonjour le monde"));
 +
     
 +
    /*
 +
      output :
 +
      bonjour le monde from www.rgagnon.com : bonjour le monde
 +
    */ 
 +
  }
 +
}
 +
</highlightsyntax>
 +
 
[[Category:Java]]
 
[[Category:Java]]

Revision as of 18:22, 17 August 2009

Implementations

Java

Since Java String are immutable (can't be changed), we return a new String. <highlightsyntax language="java122">

public static String replace (String target, String from, String to) {   
 //   target is the original string
 //   from   is the string to be replaced
 //   to     is the string which will used to replace
 //  returns a new String!
 int start = target.indexOf(from);
 if (start == -1) return target;
 int lf = from.length();
 char [] targetChars = target.toCharArray();
 StringBuffer buffer = new StringBuffer();
 int copyFrom = 0;
 while (start != -1) {
   buffer.append (targetChars, copyFrom, start-copyFrom);
   buffer.append (to);
   copyFrom = start + lf;
   start = target.indexOf (from, copyFrom);
   }
 buffer.append (targetChars, copyFrom, targetChars.length - copyFrom);
 return buffer.toString();
 }

</highlightsyntax> The String.replaceAll() method can also be used. You need to provide a regular expression. [JDK1.4] <highlightsyntax language="java122">

public class Test{
 public static void main(String[] args){
   String text = "hello world from www.rgagnon.com : hello world";
   System.out.println
     (text.replaceAll("(?:hello world)+", "bonjour le monde"));
   System.out.println
     (text.replaceFirst("(?:hello world)+", "bonjour le monde"));
     
   /*
     output :
     bonjour le monde from www.rgagnon.com : bonjour le monde
     bonjour le monde from www.rgagnon.com : hello world
   */  
 }

} </highlightsyntax> Keep in mind, that you need to escape characters like $ or | since they have special meaning when used in a regular expression.

Since JDK 1.5, the String.replace() method can also be used. It does not take a regular expression so you don't need to worry about escaping special characters. <highlightsyntax language="java122">

public class Test{
 public static void main(String[] args){
   String text = "hello world from www.rgagnon.com : hello world";
   System.out.println
     (text.replace("hello world", "bonjour le monde"));
     
   /*
     output :
     bonjour le monde from www.rgagnon.com : bonjour le monde
   */  
 }

} </highlightsyntax>