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
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON/.NET6] Information 클래스 : IsNumeric 정적 메소드를 사용해 숫자 문자열 여부 구하기 (0) | 2022.07.13 |
---|---|
[C#/COMMON/.NET6] PropertyInfo 클래스 : GetCustomAttributes 메소드를 사용해 객체 속성에 설정된 어트리뷰트 구하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] Attribute 클래스 : IsDefined 정적 메소드를 사용해 객체 속성의 어트리뷰트 설정 여부 구하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] 대소문자 구분없이 문자열 대체하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] 대소문자 구분없이 문자열 대체하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] StringBuilder 클래스 : 대소문자 구분없이 문자열 대체하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] BASE64 문자열 여부 구하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] Convert 클래스 : TryFromBase64String 정적 메소드를 사용해 BASE64 문자열 여부 구하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] Regex 클래스 : IsMatch 정적 메소드를 사용해 BASE64 문자열 여부 구하기 (0) | 2022.07.13 |
[C#/COMMON/.NET6] ReadOnlySpan<T> 구조체 : Slice 메소드 사용하기 (0) | 2022.07.11 |
댓글을 달아 주세요