함수 stringReplace() 편집하기

경고: 로그인하지 않았습니다. 편집을 하면 IP 주소가 공개되게 됩니다. 로그인하거나 계정을 생성하면 편집자가 사용자 이름으로 기록되고, 다른 장점도 있습니다.

편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.

최신판 당신의 편집
1번째 줄: 1번째 줄:
[[category: String]]
[[category: String]]
{{lowercase title}}
;gsub
;gsub
;replace
;replace
7번째 줄: 8번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<syntaxhighlight lang='bash'>
<source 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
</syntaxhighlight>
</source>
<syntaxhighlight lang='bash'>
<source 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
</syntaxhighlight>
</source>
 
==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]]
<syntaxhighlight lang="csharp">
<source 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"
</syntaxhighlight>
</source>


==Excel==
==Excel==
[[category: Excel]]
[[category: Excel]]
<syntaxhighlight lang='php'>
<source lang='php'>
=SUBSTITUTE("hello world", "hello", "yellow")
=SUBSTITUTE("hello world", "hello", "yellow")
// yellow world
// yellow world
</syntaxhighlight>
</source>
 
==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]]
<syntaxhighlight lang="java5">
<source 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"
</syntaxhighlight>
</source>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<syntaxhighlight lang="JavaScript">
<source 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"
</syntaxhighlight>
</source>


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


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


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


==PowerShell==
==PowerShell==
[[category: PowerShell]]
[[category: PowerShell]]
<syntaxhighlight lang="text">
<source 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"
</syntaxhighlight>
</source>


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


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


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


==VB==
==VB==
[[category: VB]]
[[category: VB]]
<syntaxhighlight lang="vb">
<source 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"
</syntaxhighlight>
</source>
 
==Windows Batch==
<syntaxhighlight lang='batch'>
set STR="hello world"
set OUTPUT=%STR:hello=yellow%
echo %OUTPUT%
REM yello world
</syntaxhighlight>


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

제타위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는 제타위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요.
또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다. 저작권이 있는 내용을 허가 없이 저장하지 마세요!

취소 편집 도움말 (새 창에서 열림)

이 문서에서 사용한 틀: