String.Replace 메서드

정의

현재 문자열에서 발견되는 지정된 유니코드 문자 또는 String을 모두 지정된 다른 유니코드 문자 또는 String으로 바꾼 새 문자열을 반환합니다.

오버로드

Replace(Char, Char)

현재 인스턴스의 지정된 유니코드 문자가 지정된 다른 유니코드 문자로 모두 바뀌는 새 문자열을 반환합니다.

Replace(String, String)

현재 인스턴스의 지정된 문자열이 지정된 다른 문자열로 모두 바뀌는 새 문자열을 반환합니다.

Replace(String, String, StringComparison)

제공된 비교 유형을 사용하여 현재 인스턴스의 지정된 문자열이 지정된 다른 문자열로 모두 바뀌는 새 문자열을 반환합니다.

Replace(String, String, Boolean, CultureInfo)

제공된 문화권과 대/소문자 구분을 사용하여 현재 인스턴스의 지정된 문자열이 지정된 다른 문자열로 모두 바뀌는 새 문자열을 반환합니다.

Replace(Char, Char)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

현재 인스턴스의 지정된 유니코드 문자가 지정된 다른 유니코드 문자로 모두 바뀌는 새 문자열을 반환합니다.

public:
 System::String ^ Replace(char oldChar, char newChar);
public string Replace (char oldChar, char newChar);
member this.Replace : char * char -> string
Public Function Replace (oldChar As Char, newChar As Char) As String

매개 변수

oldChar
Char

바꿀 유니코드 문자입니다.

newChar
Char

모든 oldChar을 바꿀 유니코드 문자입니다.

반환

oldChar의 모든 인스턴스를 newChar로 바꾼다는 점을 제외하고 이 인스턴스와 동일한 문자열입니다. oldChar를 현재 인스턴스에서 찾을 수 없으면 메서드가 변경되지 않은 현재 인스턴스를 반환합니다.

예제

다음 예제에서는 일련의 숫자 사이에 공백에 대한 쉼표를 대체하여 쉼표로 구분된 값 목록을 만듭니다.

using namespace System;
int main()
{
   String^ str = "1 2 3 4 5 6 7 8 9";
   Console::WriteLine( "Original string: \"{0}\"", str );
   Console::WriteLine( "CSV string:      \"{0}\"", str->Replace( ' ', ',' ) );
}

//
// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
//
String str = "1 2 3 4 5 6 7 8 9";
Console.WriteLine("Original string: \"{0}\"", str);
Console.WriteLine("CSV string:      \"{0}\"", str.Replace(' ', ','));

// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
let str = "1 2 3 4 5 6 7 8 9"
printfn $"Original string: \"{str}\""
printfn $"CSV string:      \"{str.Replace(' ', ',')}\""

// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
Class stringReplace1
   Public Shared Sub Main()
      Dim str As [String] = "1 2 3 4 5 6 7 8 9"
      Console.WriteLine("Original string: ""{0}""", str)
      Console.WriteLine("CSV string:      ""{0}""", str.Replace(" "c, ","c))
   End Sub
End Class
' This example produces the following output:
' Original string: "1 2 3 4 5 6 7 8 9"
' CSV string:      "1,2,3,4,5,6,7,8,9"

설명

이 메서드는 서수(대/소문자를 구분하고 문화권을 구분하지 않음) 검색을 수행하여 를 찾 oldChar습니다.

참고

이 메서드는 현재 instance 값을 수정하지 않습니다. 대신 의 모든 항목이 로 대체newChar되는 새 문자열을 반환합니다oldChar.

이 메서드는 수정된 문자열을 반환하므로 메서드에 대한 연속 호출을 Replace 함께 연결하여 원래 문자열에서 여러 대체를 수행할 수 있습니다. 메서드 호출은 왼쪽에서 오른쪽으로 실행됩니다. 다음 예제에서 이에 대해 설명합니다.

String s = new String('a', 3);
Console.WriteLine("The initial string: '{0}'", s);
s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd');
Console.WriteLine("The final string: '{0}'", s);

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = new string('a', 3)
printfn $"The initial string: '{s}'"
let s2 = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd')
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As New String("a"c, 3)
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a"c, "b"c).Replace("b"c, "c"c).Replace("c"c, "d"c)
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

추가 정보

적용 대상

Replace(String, String)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

현재 인스턴스의 지정된 문자열이 지정된 다른 문자열로 모두 바뀌는 새 문자열을 반환합니다.

public:
 System::String ^ Replace(System::String ^ oldValue, System::String ^ newValue);
public string Replace (string oldValue, string newValue);
public string Replace (string oldValue, string? newValue);
member this.Replace : string * string -> string
Public Function Replace (oldValue As String, newValue As String) As String

매개 변수

oldValue
String

바꿀 문자열입니다.

newValue
String

모든 oldValue를 바꿀 문자열입니다.

반환

oldValue의 모든 인스턴스를 newValue로 바꾼다는 점을 제외하고 현재 문자열과 동일한 문자열입니다. oldValue를 현재 인스턴스에서 찾을 수 없으면 메서드가 변경되지 않은 현재 인스턴스를 반환합니다.

예외

oldValue이(가) null인 경우

oldValue가 빈 문자열("")입니다.

예제

다음 예제에서는 메서드를 사용하여 Replace 맞춤법 오류를 수정하는 방법을 보여 줍니다.

using namespace System;
int main()
{
   String^ errString = "This docment uses 3 other docments to docment the docmentation";
   Console::WriteLine( "The original string is:\n'{0}'\n", errString );

   // Correct the spelling of S"document".
   String^ correctString = errString->Replace( "docment", "document" );
   Console::WriteLine( "After correcting the string, the result is:\n'{0}'", correctString );
}
//
// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//
string errString = "This docment uses 3 other docments to docment the docmentation";

Console.WriteLine("The original string is:{0}'{1}'{0}", Environment.NewLine, errString);

// Correct the spelling of "document".

string correctString = errString.Replace("docment", "document");

Console.WriteLine("After correcting the string, the result is:{0}'{1}'",
        Environment.NewLine, correctString);

// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//
open System

let errString = "This docment uses 3 other docments to docment the docmentation"

printfn $"The original string is:{Environment.NewLine}'{errString}'{Environment.NewLine}"

// Correct the spelling of "document".

let correctString = errString.Replace("docment", "document")

printfn $"After correcting the string, the result is:{Environment.NewLine}'{correctString}'"

// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//
Public Class ReplaceTest
    
    Public Shared Sub Main()
        Dim errString As String = "This docment uses 3 other docments to docment the docmentation"
                
        Console.WriteLine("The original string is:{0}'{1}'{0}", Environment.NewLine, errString)

        ' Correct the spelling of "document".  
        Dim correctString As String = errString.Replace("docment", "document")
      
        Console.WriteLine("After correcting the string, the result is:{0}'{1}'", Environment.NewLine, correctString)
    End Sub
End Class
'
' This code example produces the following output:
'
' The original string is:
' 'This docment uses 3 other docments to docment the docmentation'
'
' After correcting the string, the result is:
' 'This document uses 3 other documents to document the documentation'
'

설명

이 이nullnewValue 의 모든 항목 oldValue 이 제거됩니다.

참고

이 메서드는 현재 instance 값을 수정하지 않습니다. 대신 의 모든 항목이 로 대체newValue되는 새 문자열을 반환합니다oldValue.

이 메서드는 서수(대/소문자를 구분하고 문화권을 구분하지 않음) 검색을 수행하여 를 찾 oldValue습니다.

이 메서드는 수정된 문자열을 반환하므로 메서드에 대한 연속 호출을 Replace 함께 연결하여 원래 문자열에서 여러 대체를 수행할 수 있습니다. 메서드 호출은 왼쪽에서 오른쪽으로 실행됩니다. 다음 예제에서 이에 대해 설명합니다.

String s = "aaa";
Console.WriteLine("The initial string: '{0}'", s);
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine("The final string: '{0}'", s);

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = "aaa"
printfn $"The initial string: '{s}'"
let s2 = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As String = "aaa"
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

추가 정보

적용 대상

Replace(String, String, StringComparison)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

제공된 비교 유형을 사용하여 현재 인스턴스의 지정된 문자열이 지정된 다른 문자열로 모두 바뀌는 새 문자열을 반환합니다.

public:
 System::String ^ Replace(System::String ^ oldValue, System::String ^ newValue, StringComparison comparisonType);
public string Replace (string oldValue, string? newValue, StringComparison comparisonType);
public string Replace (string oldValue, string newValue, StringComparison comparisonType);
member this.Replace : string * string * StringComparison -> string
Public Function Replace (oldValue As String, newValue As String, comparisonType As StringComparison) As String

매개 변수

oldValue
String

바꿀 문자열입니다.

newValue
String

모든 oldValue를 바꿀 문자열입니다.

comparisonType
StringComparison

이 인스턴스 내에서 oldValue를 검색하는 방법을 결정하는 열거형 값 중 하나입니다.

반환

oldValue의 모든 인스턴스를 newValue로 바꾼다는 점을 제외하고 현재 문자열과 동일한 문자열입니다. oldValue를 현재 인스턴스에서 찾을 수 없으면 메서드가 변경되지 않은 현재 인스턴스를 반환합니다.

예외

oldValue이(가) null인 경우

oldValue가 빈 문자열("")입니다.

설명

이 이nullnewValue 의 모든 항목 oldValue 이 제거됩니다.

참고

이 메서드는 현재 instance 값을 수정하지 않습니다. 대신 의 모든 항목이 로 대체newValue되는 새 문자열을 반환합니다oldValue.

이 메서드는 에서 설명하는 문화권 및 대/소문자 구분을 사용하여 검색을 수행하여 찾 oldValue 습니다 comparisonType.

이 메서드는 수정된 문자열을 반환하므로 메서드에 대한 연속 호출을 Replace 함께 연결하여 원래 문자열에서 여러 대체를 수행할 수 있습니다. 메서드 호출은 왼쪽에서 오른쪽으로 실행됩니다. 다음 예제에서 이에 대해 설명합니다.

String s = "aaa";
Console.WriteLine("The initial string: '{0}'", s);
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine("The final string: '{0}'", s);

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = "aaa"
printfn $"The initial string: '{s}'"
let s2 = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As String = "aaa"
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

적용 대상

Replace(String, String, Boolean, CultureInfo)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

제공된 문화권과 대/소문자 구분을 사용하여 현재 인스턴스의 지정된 문자열이 지정된 다른 문자열로 모두 바뀌는 새 문자열을 반환합니다.

public:
 System::String ^ Replace(System::String ^ oldValue, System::String ^ newValue, bool ignoreCase, System::Globalization::CultureInfo ^ culture);
public string Replace (string oldValue, string? newValue, bool ignoreCase, System.Globalization.CultureInfo? culture);
public string Replace (string oldValue, string newValue, bool ignoreCase, System.Globalization.CultureInfo culture);
member this.Replace : string * string * bool * System.Globalization.CultureInfo -> string
Public Function Replace (oldValue As String, newValue As String, ignoreCase As Boolean, culture As CultureInfo) As String

매개 변수

oldValue
String

바꿀 문자열입니다.

newValue
String

모든 oldValue를 바꿀 문자열입니다.

ignoreCase
Boolean

비교할 때 대/소문자를 무시하려면 true이고, 그렇지 않으면 false입니다.

culture
CultureInfo

비교할 때 사용할 문화권입니다. culturenull이면 현재 문화권이 사용됩니다.

반환

oldValue의 모든 인스턴스를 newValue로 바꾼다는 점을 제외하고 현재 문자열과 동일한 문자열입니다. oldValue를 현재 인스턴스에서 찾을 수 없으면 메서드가 변경되지 않은 현재 인스턴스를 반환합니다.

예외

oldValue이(가) null인 경우

oldValue가 빈 문자열("")입니다.

설명

이 이nullnewValue 의 모든 항목 oldValue 이 제거됩니다.

참고

이 메서드는 현재 instance 값을 수정하지 않습니다. 대신 의 모든 항목이 로 대체newValue되는 새 문자열을 반환합니다oldValue.

이 메서드는 제공된 및 ignoreCase 대/소문자 구분을 사용하여 검색을 culture 수행하여 찾 oldValue 습니다.

이 메서드는 수정된 문자열을 반환하므로 메서드에 대한 연속 호출을 Replace 함께 연결하여 원래 문자열에서 여러 대체를 수행할 수 있습니다. 메서드 호출은 왼쪽에서 오른쪽으로 실행됩니다. 다음 예제에서 이에 대해 설명합니다.

String s = "aaa";
Console.WriteLine("The initial string: '{0}'", s);
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine("The final string: '{0}'", s);

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = "aaa"
printfn $"The initial string: '{s}'"
let s2 = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As String = "aaa"
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

적용 대상