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

 
(사용자 3명의 중간 판 22개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[category: String]]
[[category: String]]
{{lowercase title}}
;gsub
;gsub
;replace
;replace
8번째 줄: 7번째 줄:
==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++]]
{{참고|C++ 문자열 교체}}
<syntaxhighlight lang='cpp' run>
#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
}
</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>
 
==Go==
[[분류: Go]]
{{참고|Go 문자열 치환}}
<syntaxhighlight lang='go' run>
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
}
</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()}}
[[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==
112번째 줄: 162번째 줄:
===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==
<syntaxhighlight lang='batch'>
set STR="hello world"
set OUTPUT=%STR:hello=yellow%
echo %OUTPUT%
REM yello world
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[strpos]]
* [[함수 strtr()]]
*[[stringByReplacingMatchesInString]]
* [[함수 strpos()]]
*[[str_replace_file]]
* [[함수 preg_replace()]] - 정규식에 매치되는 부분문자열을 교체
*[[str_repeat]]
* [[함수 str_replace_nth()]]
*[[replaces_between]]
* [[str_replace_file()]]
* [[str_repeat]]
* [[replaces_between()]]
* [[리눅스 sed]]

2023년 10월 28일 (토) 11:37 기준 최신판

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