c#/helper

c# 다중 스페이스 제거

AUSTAR 2024. 4. 2. 11:33

확장 메서드를 이용한 방법입니다

 


/// <summary>
/// 다중 스페이스 제거
/// "a b c  d    e" To "a b c d e"
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string RemoveMultipleSpace(this string source)
{
    return string.IsNullOrWhiteSpace(source) ? "" : Regex.Replace(source, @"\s+", $"{(char)32}");
}

 

출처 : https://www.naveen.com.au/asp-net/to-replace-multiple-spaces-with-a-single-space-in-csharp-vbnet/361