- gsub
- replace
- sed
- str_replace
1 Bash[ | ]
Bash
Copy
STR="hello world"
OUTPUT=`echo $STR | sed 's/hello/yellow/'`
echo $OUTPUT
# yellow world
Bash
Copy
STR="hello world"
OUTPUT=`echo $STR | sed 's/hello/yellow/g'`
echo $OUTPUT
# yellow world
2 C++[ | ]

C++
Copy
#include <iostream>
using namespace std;
void str_replace(string& s, string const& search, string const& replace) {
string buf;
size_t pos = 0;
size_t prevPos;
buf.reserve(s.size());
while (true) {
prevPos = pos;
pos = s.find(search, pos);
if (pos == string::npos) {
break;
}
buf.append(s, prevPos, pos - prevPos);
buf += replace;
pos += search.size();
}
buf.append(s, prevPos, s.size() - prevPos);
s.swap(buf);
}
int main() {
string s = "hello world hello";
str_replace(s, "hello", "yellow");
cout << s; // yellow world yellow
}
Loading
3 C#[ | ]
C#
Copy
"effffff".Replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump"
"blah".Replace("z", "y"); // returns "blah"
4 Excel[ | ]
PHP
Copy
=SUBSTITUTE("hello world", "hello", "yellow")
// yellow world
5 Go[ | ]

Go
Copy
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Replace("hello world", "hello", "yellow", -1)) // yellow world
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) // moo moo moo
}
Loading
6 Java[ | ]
Java
Copy
"effffff".replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump"
"effffff".replaceAll("f*", "jump"); // returns "ejump"
7 JavaScript[ | ]
JavaScript
Copy
"effffff".replace('f','jump'); // returns "ejumpfffff"
"effffff".replace(/f/g,'jump'); // returns "ejumpjumpjumpjumpjumpjump"
8 Objective-C[ | ]
Objective-C
Copy
[@"effffff" stringByReplacingOccurrencesOfString:@"f" withString:@"jump"] // returns "ejumpjumpjumpjumpjumpjump"
9 Perl[ | ]
Perl
Copy
$b = "effffff";
$b =~ s/f/jump/g;
# $b is "ejumpjumpjumpjumpjumpjump"
10 PHP[ | ]

PHP
Copy
$b = str_replace("f", "jump", "effffff");
// returns "ejumpjumpjumpjumpjumpjump"
11 PowerShell[ | ]
text
Copy
"effffff" -replace "f", "jump" # returns "ejumpjumpjumpjumpjumpjump"
"effffff" -replace "f*", "jump" # returns "ejump"
12 Python[ | ]
Python
Copy
"effffff".replace("f", "jump")
# returns "ejumpjumpjumpjumpjumpjump"
13 Ruby[ | ]
Ruby
Copy
puts "effffff".gsub("f", "jump")
# ejumpjumpjumpjumpjumpjump
Ruby
Copy
puts "effffff".gsub(/f/, "jump")
# ejumpjumpjumpjumpjumpjump
Ruby
Copy
str = "effffff"
str2 = str.gsub("f", "jump")
puts str2
Ruby
Copy
str = "effffff"
str.gsub!("f", "jump")
puts str
14 SQL[ | ]
14.1 MS-SQL[ | ]
- See #MySQL
14.2 MySQL[ | ]
sql
Copy
SELECT REPLACE("hello world", "hello", "yellow");
-- yellow world
15 VB[ | ]
vbnet
Copy
Replace("effffff", "f", "jump") ' returns "ejumpjumpjumpjumpjumpjump"
Replace("blah", "z", "y") ' returns "blah"
16 Windows Batch[ | ]
batch
Copy
set STR="hello world"
set OUTPUT=%STR:hello=yellow%
echo %OUTPUT%
REM yello world
17 같이 보기[ | ]
- 함수 strtr()
- 함수 strpos()
- 함수 preg_replace() - 정규식에 매치되는 부분문자열을 교체
- 함수 str_replace_nth()
- str_replace_file()
- str_repeat
- replaces_between()
- 리눅스 sed
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.