본문 바로가기
프로그래밍/C#

[C#] C# string.Replace 메서드

by 불타는홍당무 2019. 9. 26.

public string Replace (char oldChar, char newChar);

 

oldChar를 newChar로 치환하여 반환한다. oldChar가 없으면 기존 값을 반환한다.

현재의 인스턴스 값은 수정되지 않는다.

using System;

public class Example
{
   public static void Main()
   {
      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 initial string: 'aaa'

The final string: ‘ddd'

 

 

public string Replace (string oldValue, string newValue);

 

oldValue를 newValue로 치환하여 반환한다. oldChar가 없으면 기존 값을 반환한다.

현재의 인스턴스 값은 수정되지 않는다.

using System;

public class ReplaceTest {
    public static void Main() {
        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 docment uses 3 other docments to docment the docmentation

This document uses 3 other documents to document the documentation

 

'프로그래밍 > C#' 카테고리의 다른 글

[C#] C# string.Substring 메서드  (0) 2019.09.26
[C#] List 함수를 이용한 Linq 메서드식 표현  (0) 2018.11.15
[C#] List 주요 함수 정리  (0) 2018.11.15