String.Replace 메서드

정의

지정한 유니코드 문자나 현재 문자열의 모든 항목이 지정된 다른 유니코드 문자 또는 StringString다른 문자열로 대체되는 새 문자열을 반환합니다.

오버로드

Name Description
Replace(Char, Char)

이 인스턴스에서 지정된 유니코드 문자가 모두 지정된 다른 유니코드 문자로 대체되는 새 문자열을 반환합니다.

Replace(String, String)

현재 인스턴스에서 지정된 문자열이 모두 지정된 다른 문자열로 대체되는 새 문자열을 반환합니다.

Replace(Rune, Rune)
Replace(String, String, StringComparison)

제공된 비교 형식을 사용하여 현재 인스턴스에서 지정된 문자열이 모두 다른 지정된 문자열로 대체되는 새 문자열을 반환합니다.

Replace(String, String, Boolean, CultureInfo)

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

Replace(Char, Char)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
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.

반품

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

예제

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

string str = "1 2 3 4 5 6 7 8 9";
Console.WriteLine($"Original string: \"{str}\"");
Console.WriteLine($"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"
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습니다.

메모

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

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

string s = new('a', 3);
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd');
Console.WriteLine($"The final string: '{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
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.

반품

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

예외

oldValuenull입니다.

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

예제

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

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

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

// Correct the spelling of "document".
string correctString = errString.Replace("docment", "document");

Console.WriteLine($"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'
//
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'
'

설명

newValue경우 null 모든 항목이 oldValue 제거됩니다.

메모

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

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

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

string s = "aaa";
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine($"The final string: '{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(Rune, Rune)

Source:
String.Manipulation.cs
public:
 System::String ^ Replace(System::Text::Rune oldRune, System::Text::Rune newRune);
public string Replace(System.Text.Rune oldRune, System.Text.Rune newRune);
member this.Replace : System.Text.Rune * System.Text.Rune -> string
Public Function Replace (oldRune As Rune, newRune As Rune) As String

매개 변수

oldRune
Rune
newRune
Rune

반품

적용 대상

Replace(String, String, StringComparison)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
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 결정하는 열거형 값 중 하나입니다.

반품

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

예외

oldValuenull입니다.

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

설명

newValue경우 null 모든 항목이 oldValue 제거됩니다.

메모

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

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

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

string s = "aaa";
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine($"The final string: '{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
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인 경우, 현재 문화권이 적용됩니다.

반품

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

예외

oldValuenull입니다.

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

설명

newValue경우 null 모든 항목이 oldValue 제거됩니다.

메모

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

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

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

string s = "aaa";
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine($"The final string: '{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'

적용 대상