Skip to content

Instantly share code, notes, and snippets.

@elringus
Created February 2, 2018 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elringus/46699e600418e0b3c4ac23cba385dfff to your computer and use it in GitHub Desktop.
Save elringus/46699e600418e0b3c4ac23cba385dfff to your computer and use it in GitHub Desktop.
/// <summary>
/// Checks whether string is null, empty or consists of whitespace chars.
/// </summary>
public static bool IsNullEmptyOrWhiteSpace (string content)
{
if (String.IsNullOrEmpty(content))
return true;
return String.IsNullOrEmpty(content.TrimFull());
}
/// <summary>
/// Performes <see cref="string.Trim"/> additionally removing any BOM and other service symbols.
/// </summary>
public static string TrimFull (this string source)
{
#if UNITY_WEBGL // WebGL build under .NET 4.6 fails when using Trim with UTF-8 chars. (should be fixed in Unity 2018.1)
var whitespaceChars = new List<char> {
'\u0009','\u000A','\u000B','\u000C','\u000D','\u0020','\u0085','\u00A0',
'\u1680','\u2000','\u2001','\u2002','\u2003','\u2004','\u2005','\u2006',
'\u2007','\u2008','\u2009','\u200A','\u2028','\u2029','\u202F','\u205F',
'\u3000','\uFEFF','\u200B',
};
// Trim start.
if (string.IsNullOrEmpty(source)) return source;
var c = source[0];
while (whitespaceChars.Contains(c))
{
if (source.Length <= 1) return string.Empty;
source = source.Substring(1);
c = source[0];
}
// Trim end.
if (string.IsNullOrEmpty(source)) return source;
c = source[source.Length - 1];
while (whitespaceChars.Contains(c))
{
if (source.Length <= 1) return string.Empty;
source = source.Substring(0, source.Length - 1);
c = source[source.Length - 1];
}
return source;
#else
return source.Trim().Trim(new char[] { '\uFEFF', '\u200B' });
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment