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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
8번째 줄: 8번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
STR="hello world"
STR="hello world"
OUTPUT=`echo $STR | sed 's/hello/yellow/'`
OUTPUT=`echo $STR | sed 's/hello/yellow/'`
echo $OUTPUT
echo $OUTPUT
# yellow world
# yellow world
</source>
</syntaxhighlight>
<source lang='bash'>
<syntaxhighlight lang='bash'>
STR="hello world"
STR="hello world"
OUTPUT=`echo $STR | sed 's/hello/yellow/g'`
OUTPUT=`echo $STR | sed 's/hello/yellow/g'`
echo $OUTPUT
echo $OUTPUT
# yellow world
# yellow world
</source>
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang="csharp">
<syntaxhighlight lang="csharp">
"effffff".Replace("f", "jump");    // returns "ejumpjumpjumpjumpjumpjump"
"effffff".Replace("f", "jump");    // returns "ejumpjumpjumpjumpjumpjump"
"blah".Replace("z", "y");          // returns "blah"
"blah".Replace("z", "y");          // returns "blah"
</source>
</syntaxhighlight>


==Excel==
==Excel==
[[category: Excel]]
[[category: Excel]]
<source lang='php'>
<syntaxhighlight lang='php'>
=SUBSTITUTE("hello world", "hello", "yellow")
=SUBSTITUTE("hello world", "hello", "yellow")
// yellow world
// yellow world
</source>
</syntaxhighlight>


==Java==
==Java==
[[category: Java]]
[[category: Java]]
<source lang="java5">
<syntaxhighlight lang="java5">
"effffff".replace("f", "jump");    // returns "ejumpjumpjumpjumpjumpjump"
"effffff".replace("f", "jump");    // returns "ejumpjumpjumpjumpjumpjump"
"effffff".replaceAll("f*", "jump"); // returns "ejump"
"effffff".replaceAll("f*", "jump"); // returns "ejump"
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang="JavaScript">
<syntaxhighlight lang="JavaScript">
"effffff".replace('f','jump');  // returns "ejumpfffff"
"effffff".replace('f','jump');  // returns "ejumpfffff"
"effffff".replace(/f/g,'jump');  // returns "ejumpjumpjumpjumpjumpjump"
"effffff".replace(/f/g,'jump');  // returns "ejumpjumpjumpjumpjumpjump"
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang="objc">
<syntaxhighlight lang="objc">
[@"effffff" stringByReplacingOccurrencesOfString:@"f" withString:@"jump"]    // returns "ejumpjumpjumpjumpjumpjump"
[@"effffff" stringByReplacingOccurrencesOfString:@"f" withString:@"jump"]    // returns "ejumpjumpjumpjumpjumpjump"
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang="perl">
<syntaxhighlight lang="perl">
$b = "effffff";
$b = "effffff";
$b =~ s/f/jump/g;
$b =~ s/f/jump/g;
# $b is "ejumpjumpjumpjumpjumpjump"
# $b is "ejumpjumpjumpjumpjumpjump"
</source>
</syntaxhighlight>


==PHP==
==PHP==
{{참고|PHP str_replace()}}
{{참고|PHP str_replace()}}
[[category: PHP]]
[[category: PHP]]
<source lang="php">
<syntaxhighlight lang="php">
$b = str_replace("f", "jump", "effffff");
$b = str_replace("f", "jump", "effffff");
// returns "ejumpjumpjumpjumpjumpjump"
// returns "ejumpjumpjumpjumpjumpjump"
</source>
</syntaxhighlight>


==PowerShell==
==PowerShell==
[[category: PowerShell]]
[[category: PowerShell]]
<source lang="text">
<syntaxhighlight lang="text">
"effffff" -replace "f", "jump"      #  returns "ejumpjumpjumpjumpjumpjump"
"effffff" -replace "f", "jump"      #  returns "ejumpjumpjumpjumpjumpjump"
"effffff" -replace "f*", "jump"    #  returns "ejump"
"effffff" -replace "f*", "jump"    #  returns "ejump"
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang="python">
<syntaxhighlight lang="python">
"effffff".replace("f", "jump")
"effffff".replace("f", "jump")
# returns "ejumpjumpjumpjumpjumpjump"
# returns "ejumpjumpjumpjumpjumpjump"
</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang="ruby">
<syntaxhighlight lang="ruby">
puts "effffff".gsub("f", "jump")
puts "effffff".gsub("f", "jump")
# ejumpjumpjumpjumpjumpjump
# ejumpjumpjumpjumpjumpjump
</source>
</syntaxhighlight>
<source lang="ruby">
<syntaxhighlight lang="ruby">
puts "effffff".gsub(/f/, "jump")
puts "effffff".gsub(/f/, "jump")
# ejumpjumpjumpjumpjumpjump
# ejumpjumpjumpjumpjumpjump
</source>
</syntaxhighlight>
<source lang="ruby">
<syntaxhighlight lang="ruby">
str = "effffff"
str = "effffff"
str2 = str.gsub("f", "jump")  
str2 = str.gsub("f", "jump")  
puts str2
puts str2
</source>
</syntaxhighlight>
<source lang="ruby">
<syntaxhighlight lang="ruby">
str = "effffff"
str = "effffff"
str.gsub!("f", "jump")  
str.gsub!("f", "jump")  
puts str
puts str
</source>
</syntaxhighlight>


==SQL==
==SQL==
113번째 줄: 113번째 줄:
===MySQL===
===MySQL===
[[category: MySQL]]
[[category: MySQL]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT REPLACE("hello world", "hello", "yellow");
SELECT REPLACE("hello world", "hello", "yellow");
-- yellow world
-- yellow world
</source>
</syntaxhighlight>


==VB==
==VB==
[[category: VB]]
[[category: VB]]
<source lang="vb">
<syntaxhighlight lang="vb">
Replace("effffff", "f", "jump")    '  returns "ejumpjumpjumpjumpjumpjump"
Replace("effffff", "f", "jump")    '  returns "ejumpjumpjumpjumpjumpjump"
Replace("blah", "z", "y")          '  returns "blah"
Replace("blah", "z", "y")          '  returns "blah"
</source>
</syntaxhighlight>


==Windows Batch==
==Windows Batch==
<source lang='batch'>
<syntaxhighlight lang='batch'>
set STR="hello world"
set STR="hello world"
set OUTPUT=%STR:hello=yellow%
set OUTPUT=%STR:hello=yellow%
echo %OUTPUT%
echo %OUTPUT%
REM yello world
REM yello world
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:33 판


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 Java

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

5 JavaScript

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

6 Objective-C

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

7 Perl

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

8 PHP

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

9 PowerShell

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

10 Python

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

11 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

12 SQL

12.1 MS-SQL

12.2 MySQL

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

13 VB

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

14 Windows Batch

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

15 같이 보기