Last active
September 19, 2023 14:48
-
-
Save nagasato/1f4473645b623c233fe09170b5163421 to your computer and use it in GitHub Desktop.
TrimEx method to remove control characters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace StringUtilities | |
{ | |
/// <summary> | |
/// StringExtensions | |
/// </summary> | |
public static class StringExtensions | |
{ | |
/// <summary> | |
/// 文字列から制御文字を除去した文字列を取得する | |
/// </summary> | |
/// <param name="s"></param> | |
/// <returns></returns> | |
public static string RemoveControlChars(this string s) | |
{ | |
if (s == null) { throw new ArgumentNullException(); } | |
if (string.IsNullOrEmpty(s)) { return s; } | |
var ss = s.Where(c => !char.IsControl(c)).ToArray(); // 制御文字を除去 | |
return new string(ss); | |
} | |
/// <summary> | |
/// 文字列の先頭・末尾の両端の制御文字と空白を除去した文字列を取得する | |
/// </summary> | |
/// <returns></returns> | |
public static string TrimEx(this string s) | |
{ | |
if (s == null) { throw new ArgumentNullException(); } | |
if (string.IsNullOrEmpty(s)) { return s; } | |
var ss = s.TrimStartEx(); | |
ss = ss.TrimEndEx(); | |
return ss; | |
} | |
/// <summary> | |
/// 文字列の先頭側の制御文字と空白を除去した文字列を取得する | |
/// </summary> | |
/// <param name="s"></param> | |
/// <returns></returns> | |
public static string TrimStartEx(this string s) | |
{ | |
if (s == null) { throw new ArgumentNullException(); } | |
if (string.IsNullOrEmpty(s)) { return s; } | |
IEnumerable<char> removeChars = EnumerateStartControlAndSpaceChars(s); | |
return s.TrimStart(removeChars.ToArray()); | |
} | |
/// <summary> | |
/// 文字列の末尾側の制御文字と空白を除去した文字列を取得する | |
/// </summary> | |
/// <param name="s"></param> | |
/// <returns></returns> | |
public static string TrimEndEx(this string s) | |
{ | |
if (s == null) { throw new ArgumentNullException(); } | |
if (string.IsNullOrEmpty(s)) { return s; } | |
IEnumerable<char> removeChars = EnumerateEndControlAndSpaceChars(s); | |
return s.TrimEnd(removeChars.ToArray()); | |
} | |
/// <summary> | |
/// 先頭側の制御文字と空白文字を列挙する | |
/// </summary> | |
/// <param name="chars"></param> | |
/// <returns></returns> | |
private static IEnumerable<char> EnumerateStartControlAndSpaceChars(IEnumerable<char> chars) | |
{ | |
foreach (char c in chars) | |
{ | |
if (char.IsControl(c) || c == ' ') | |
{ | |
yield return c; | |
continue; | |
} | |
yield break; | |
} | |
} | |
/// <summary> | |
/// 末尾側の制御文字と空白文字を列挙する | |
/// </summary> | |
/// <param name="chars"></param> | |
/// <returns></returns> | |
private static IEnumerable<char> EnumerateEndControlAndSpaceChars(IEnumerable<char> chars) | |
{ | |
var chars2 = chars.ToArray(); | |
for (var i = chars2.Length - 1; i < 0; i--) | |
{ | |
char c = chars2[i]; | |
if (char.IsControl(c) || c == ' ') | |
{ | |
yield return c; | |
continue; | |
} | |
yield break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment