728x90
반응형
728x170
▶ 대소문자 구분없이 문자열 대체하기 예제
string source = "Hello hello";
Console.WriteLine(ReplaceString(source, "hello", "morning", StringComparison.Ordinal ));
Console.WriteLine(ReplaceString(source, "hello", "morning", StringComparison.OrdinalIgnoreCase));
728x90
▶ 대소문자 구분없이 문자열 대체하기
#region 문자열 대체하기 - ReplaceString(source, previousValue, newValue, comparison)
/// <summary>
/// 문자열 대체하기
/// </summary>
/// <param name="source">소스 문자열</param>
/// <param name="previousValue">이전 값</param>
/// <param name="newValue">신규 값</param>
/// <param name="comparison">문자열 비교 방법</param>
/// <returns>대체 문자열</returns>
public string ReplaceString(string source, string previousValue, string newValue, StringComparison comparison)
{
int previousPosition = 0;
string target = source;
int position = target.IndexOf(previousValue, comparison);
while(position > -1)
{
target = target.Remove(position, previousValue.Length);
target = target.Insert(position, newValue);
previousPosition = position + newValue.Length;
position = target.IndexOf(previousValue, previousPosition, comparison);
}
return target;
}
#endregion
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON/.NET6] dynamic 타입 객체의 속성 값을 동적으로 구하기 (0) | 2022.07.14 |
---|---|
[C#/COMMON/.NET6] Char 구조체 : IsDigit 메소드를 사용해 숫자 문자열 여부 구하기 (0) | 2022.07.13 |
[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] Regex 클래스 : 대소문자 구분없이 문자열 대체하기 (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 |
댓글을 달아 주세요