첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ Regex 클래스 : 대소문자 구분없이 문자열 대체하기 예제

string source = "Hello hello";

Console.WriteLine(ReplaceString(source, "hello", "morning", RegexOptions.CultureInvariant));
Console.WriteLine(ReplaceString(source, "hello", "morning", RegexOptions.IgnoreCase      ));

 

728x90

 

▶ Regex 클래스 : 대소문자 구분없이 문자열 대체하기

using System.Text.RegularExpressions;

#region 문자열 대체하기 - ReplaceString(source, previousValue, newValue, comparison)

/// <summary>
/// 문자열 대체하기
/// </summary>
/// <param name="source">소스 문자열</param>
/// <param name="previousValue">이전 값</param>
/// <param name="newValue">신규 값</param>
/// <param name="options">문자열 비교 방법</param>
/// <returns>대체 문자열</returns>
public static string ReplaceString(string source, string previousValue, string newValue, RegexOptions options)
{
    return Regex.Replace(source, Regex.Escape(previousValue), Regex.Replace(newValue, "\\$[0-9]+", @"$$$0"), options);
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요