함수 stringReplace()

(Replace에서 넘어옴)
gsub
replace
sed
str_replace

1 Bash[ | ]

STR="hello world"
OUTPUT=`echo $STR | sed 's/hello/yellow/'`
echo $OUTPUT
# yellow world
STR="hello world"
OUTPUT=`echo $STR | sed 's/hello/yellow/g'`
echo $OUTPUT
# yellow world

2 C++[ | ]

#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
}

3 C#[ | ]

"effffff".Replace("f", "jump");     // returns "ejumpjumpjumpjumpjumpjump"
"blah".Replace("z", "y");           // returns "blah"

4 Excel[ | ]

=SUBSTITUTE("hello world", "hello", "yellow")
// yellow world

5 Go[ | ]

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
}

6 Java[ | ]

"effffff".replace("f", "jump");     // returns "ejumpjumpjumpjumpjumpjump"
"effffff".replaceAll("f*", "jump"); // returns "ejump"

7 JavaScript[ | ]

"effffff".replace('f','jump');  // returns "ejumpfffff"
"effffff".replace(/f/g,'jump');  // returns "ejumpjumpjumpjumpjumpjump"

8 Objective-C[ | ]

[@"effffff" stringByReplacingOccurrencesOfString:@"f" withString:@"jump"]     // returns "ejumpjumpjumpjumpjumpjump"

9 Perl[ | ]

$b = "effffff";
$b =~ s/f/jump/g;
# $b is "ejumpjumpjumpjumpjumpjump"

10 PHP[ | ]

$b = str_replace("f", "jump", "effffff");
// returns "ejumpjumpjumpjumpjumpjump"

11 PowerShell[ | ]

"effffff" -replace "f", "jump"      #  returns "ejumpjumpjumpjumpjumpjump"
"effffff" -replace "f*", "jump"     #  returns "ejump"

12 Python[ | ]

"effffff".replace("f", "jump")
# returns "ejumpjumpjumpjumpjumpjump"

13 Ruby[ | ]

puts "effffff".gsub("f", "jump")
# ejumpjumpjumpjumpjumpjump
puts "effffff".gsub(/f/, "jump")
# ejumpjumpjumpjumpjumpjump
str = "effffff"
str2 = str.gsub("f", "jump") 
puts str2
str = "effffff"
str.gsub!("f", "jump") 
puts str

14 SQL[ | ]

14.1 MS-SQL[ | ]

14.2 MySQL[ | ]

SELECT REPLACE("hello world", "hello", "yellow");
-- yellow world

15 VB[ | ]

Replace("effffff", "f", "jump")     '  returns "ejumpjumpjumpjumpjumpjump"
Replace("blah", "z", "y")           '  returns "blah"

16 Windows Batch[ | ]

set STR="hello world"
set OUTPUT=%STR:hello=yellow%
echo %OUTPUT%
REM yello world

17 같이 보기[ | ]