Difference between revisions of "Replace or remove all occurrences of a string"

From CodeCodex

(Ruby)
(Erlang)
Line 1: Line 1:
 +
{{Infobox See Also String}}
 
==Implementations==
 
==Implementations==
 +
 +
===Erlang===
 +
<pre>
 +
S = "replace both x and x with a y",
 +
re:replace(S, "x", "y", [global, {return, list}]).
 +
</pre>
  
 
===Java===
 
===Java===
 
Java has a built in function for replacing all occurrences of a string.  This function accepts regular expressions, so you have to take care to escape characters that might have special meaning.
 
Java has a built in function for replacing all occurrences of a string.  This function accepts regular expressions, so you have to take care to escape characters that might have special meaning.
<highlightsyntax language="java122">
+
<pre class="java">
 
  String s = "replace both x and x with a y";
 
  String s = "replace both x and x with a y";
 
  int numberOfCommas = s.replaceAll("x","y");
 
  int numberOfCommas = s.replaceAll("x","y");
</highlightsyntax>
+
</pre>
  
 
Since Java 1.5, the following is a replace method that does not use regular expressions:
 
Since Java 1.5, the following is a replace method that does not use regular expressions:
<highlightsyntax language="java122">
+
<pre class="java">
 
  String s = "replace both x and x with a y";
 
  String s = "replace both x and x with a y";
 
  int numberOfCommas = s.replace("x","y");
 
  int numberOfCommas = s.replace("x","y");
</highlightsyntax>
+
</pre>
  
 
===JavaScript===
 
===JavaScript===
 
String does not have a replaceAll function in JavaScript.  To create such a function, use the code below.  This function accepts regular expressions.
 
String does not have a replaceAll function in JavaScript.  To create such a function, use the code below.  This function accepts regular expressions.
<highlightsyntax language="javascript">
+
<pre class="javascript">
 
  String.prototype.replaceAll=function(s1, s2) {  
 
  String.prototype.replaceAll=function(s1, s2) {  
 
   return this.replace(new RegExp(s1,"g"), s2);
 
   return this.replace(new RegExp(s1,"g"), s2);
Line 29: Line 36:
 
     return (str);
 
     return (str);
 
}
 
}
</highlightsyntax>
+
</pre>
 
To use this code:
 
To use this code:
<highlightsyntax language="javascript">
+
<pre class="javascript">
 
  text = 'replace both x and x with a y';
 
  text = 'replace both x and x with a y';
 
  finalText = text.replaceAll('[x]','y');
 
  finalText = text.replaceAll('[x]','y');
</highlightsyntax>
+
</pre>
  
 
===OCaml===
 
===OCaml===
Line 46: Line 53:
 
===Perl===
 
===Perl===
 
This function accepts regular expressions.
 
This function accepts regular expressions.
<HIGHLIGHTSYNTAX language="perl">
+
<pre class="perl">
 
s{1337}{Elite}g; # replace globally
 
s{1337}{Elite}g; # replace globally
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===PHP===
 
===PHP===
<highlightsyntax language="php3">
+
<pre class="php">
 
   $new_str = str_replace($remove_this, '', $input);
 
   $new_str = str_replace($remove_this, '', $input);
</highlightsyntax>
+
</pre>
  
 
===Python===
 
===Python===
<highlightsyntax language="python">
+
<pre class="python">
 
new_str = input.replace("x", "y")
 
new_str = input.replace("x", "y")
</highlightsyntax>
+
</pre>
  
 
===Ruby===
 
===Ruby===
 
This function accepts regular expressions.
 
This function accepts regular expressions.
<pre>
+
<pre class="ruby">
 
str = "replace both x and x with a y"
 
str = "replace both x and x with a y"
 
new_str = str.gsub('x', 'y') # replace globally
 
new_str = str.gsub('x', 'y') # replace globally
Line 69: Line 76:
 
===Seed7===
 
===Seed7===
 
<pre>
 
<pre>
var string: s is "replace both x and x with a y";
+
var string: s is "replace both x and x with a y";
</pre>
+
s := replace(s, "x", "y");
<pre>
+
s := replace(s, "x", "y");
+
 
</pre>
 
</pre>
  
[[Category:JavaScript]]
+
[[Category:String]]
 +
 
 +
[[Category:Erlang]]
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:JavaScript]]
 
[[Category:Objective Caml]]
 
[[Category:Objective Caml]]
 
[[Category:Perl]]
 
[[Category:Perl]]
[[Category:Python]]
 
[[Category:Seed7]]
 
[[Category:String]]
 
 
[[Category:PHP]]
 
[[Category:PHP]]
 +
[[Category:Python]]
 
[[Category:Ruby]]
 
[[Category:Ruby]]
 +
[[Category:Seed7]]

Revision as of 15:23, 3 December 2010

Related content:

Implementations

Erlang

S = "replace both x and x with a y",
re:replace(S, "x", "y", [global, {return, list}]).

Java

Java has a built in function for replacing all occurrences of a string. This function accepts regular expressions, so you have to take care to escape characters that might have special meaning.

 String s = "replace both x and x with a y";
 int numberOfCommas = s.replaceAll("x","y");

Since Java 1.5, the following is a replace method that does not use regular expressions:

 String s = "replace both x and x with a y";
 int numberOfCommas = s.replace("x","y");

JavaScript

String does not have a replaceAll function in JavaScript. To create such a function, use the code below. This function accepts regular expressions.

 String.prototype.replaceAll=function(s1, s2) { 
   return this.replace(new RegExp(s1,"g"), s2);
 }
 String.prototype.replaceAll=function(s1, s2) {
    var str = this;
    var pos = str.indexOf(s1);
    while (pos > -1){
		str = str.replace(s1, s2);
		pos = str.indexOf(s1);
	}
    return (str);
}

To use this code:

 text = 'replace both x and x with a y';
 finalText = text.replaceAll('[x]','y');

OCaml

# let replace input output =
    Str.global_replace (Str.regexp_string input) output;;
val replace : string -> string -> string -> string = <fun>

For example:

# replace "133t" "Elite" "133t H4x0r";;
- : string = "Elite H4x0r"

Perl

This function accepts regular expressions.

s{1337}{Elite}g; # replace globally

PHP

  $new_str = str_replace($remove_this, '', $input);

Python

new_str = input.replace("x", "y")

Ruby

This function accepts regular expressions.

str = "replace both x and x with a y"
new_str = str.gsub('x', 'y') # replace globally

Seed7

var string: s is "replace both x and x with a y";
s := replace(s, "x", "y");