함수 str_replace()

Jmnote (토론 | 기여)님의 2022년 1월 6일 (목) 18:31 판 (Jmnote님이 함수 str replace() 문서를 함수 stringReplace() 문서로 이동했습니다)


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#

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

3 Excel

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

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

5 Java

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

6 JavaScript

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

7 Objective-C

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

8 Perl

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

9 PHP

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

10 PowerShell

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

11 Python

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

12 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

13 SQL

13.1 MS-SQL

13.2 MySQL

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

14 VB

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

15 Windows Batch

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

16 같이 보기