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
/// <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