■ Regex 클래스를 사용해 대소문자 구분없이 문자열을 대체하는 방법을 보여준다.
▶ Regex 클래스 : 대소문자 구분없이 문자열 대체하기 예제 (C#)
1 2 3 4 5 6 |
string source = "Hello hello"; Console.WriteLine(ReplaceString(source, "hello", "morning", RegexOptions.CultureInvariant)); Console.WriteLine(ReplaceString(source, "hello", "morning", RegexOptions.IgnoreCase )); |
▶ Regex 클래스 : 대소문자 구분없이 문자열 대체하기 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 |