본문 바로가기
C#문법/문자열

[C#문법] 문자열 - Replace :: LAZY DEVELOPER

by LAZY모닝 2024. 1. 14.
반응형

 

※ 요약
현재 문자열에서 지정된 문자 또는 문자열을 다른 지정된 문자 또는 문자열로 바꿈
 
※ 함수사용 및 설명
 
Replace (char oldChar, char newChar)
//oldChar:바꾸고자 하는 문자
//newChar:모든 oldChar를 바꿀 문자
지정된 문자를 다른 지정된 문자로 모두 바꿈
Replace (string oldValue, string newValue)
//oldValue:바꾸고자 하는 문자열
//newValue:모든 oldValue를 바꿀 문자열
지정된 문자열을 다른 지정된 문자열로 모두 바꿈
Replace (string oldValue, string newValue, StringComparison comparsonType)
//oldValue:바꾸고자 하는 문자열
//newValue:모든 oldValue를 바꿀 문자열
//comparsonType:oldValue를 검색하는 방법
제공된 비교유형을 사용하여, 지정된 문자열을 다른 지정된 문자열로 바꿈
Replace (string oldValue, string newValue, bool ignoreCase, CultureInfo culture)
//oldValue:바꾸고자 하는 문자열
//newValue:모든 oldValue를 바꿀 문자열
//ignoreCase:대/소문자를 무시하려면 true, 그렇지 않으면 false
//culture : 
제공된 비교유형과 대/소문자 구분을 사용하여 지정된 문자열을 다른 지정된 문자열로 바꿈
 
※ 예제
[코드예제]
using System.Globalization;

string standardSentence = "I like apple";
char ch1 = 'a';
char ch2 = 'b';
string str1 = "apple";
string str2 = "banana";
string str3 = "APPLE";

Console.WriteLine("{0}", standardSentence.Replace(ch1, ch2));
Console.WriteLine("{0}", standardSentence.Replace(str1, str2));
Console.WriteLine("{0}", standardSentence.Replace(str1, str2, StringComparison.OrdinalIgnoreCase));
Console.WriteLine("{0}", standardSentence.Replace(str3, str2, true, CultureInfo.CurrentCulture));
 
[실행결과]
 

 

반응형

'C#문법 > 문자열' 카테고리의 다른 글

[C#] IndexOf - 문자열 위치 찾기  (0) 2024.01.16
[C#문법] 문자열 - Contains :: LAZY DEVELOPER  (0) 2024.01.13