"함수 stringReplace()"의 두 판 사이의 차이

22번째 줄: 22번째 줄:
==C++==
==C++==
[[분류: C++]]
[[분류: C++]]
{{참고|C++ 문자열 replace()}}
{{참고|C++ 문자열 교체}}
<syntaxhighlight lang='cpp'>
<syntaxhighlight lang='cpp'>
</syntaxhighlight>
</syntaxhighlight>

2023년 10월 28일 (토) 11:23 판

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++

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 같이 보기