/// <summary> /// Check text contains forbidden text. /// </summary> /// <param name="text">a text which needs to check</param> /// <param name="isNormalizeWhitespaces">normalize whitespaces before handling</param> /// <param name="isIgnoreCase">ignore case while finding forbidden words</param> public static bool ExistForbiddenWord(string text, bool isNormalizeWhitespaces = false, bool isIgnoreCase = true) { if (mForbiddenTexts == null) { LoadData(); } if (text[0] == ' ' && text.Trim().Length == 0) { return(true); } if (isNormalizeWhitespaces) { text = MyUtilities.NormalizeWhitespaces(text); } StringComparison comparison = isIgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; string textForComparison = isIgnoreCase ? text.ToLower() : text; int count = mForbiddenTexts.Length; for (int i = 0; i < count; i++) { if (textForComparison.IndexOf(mForbiddenTexts[i], comparison) >= 0) { return(true); } } return(false); }
/// <summary> /// Find and replace forbidden words in a text. /// </summary> /// <param name="text">a text which needs to handle</param> /// <param name="isTrim">trim text before handling</param> /// <param name="isNormalizeWhitespaces">normalize whitespaces before handling</param> /// <param name="isIgnoreCase">ignore case while finding forbidden words</param> /// <param name="charReplace">a string which replaces every illegal character in forbidden word</param> public static string ReplaceForbiddenWords(string text, bool isTrim = true, bool isNormalizeWhitespaces = false, bool isIgnoreCase = true, string charReplace = "*") { if (mForbiddenTexts == null) { LoadData(); } if (isTrim) { text = text.Trim(); } if (isNormalizeWhitespaces) { text = MyUtilities.NormalizeWhitespaces(text); } return(_ReplaceForbiddenWords(text, charReplace, isIgnoreCase)); }