using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Yaulw.Tools { /// /// Useful String Operations /// public static class StringTool { /// /// Searches for the 'value' occurences that occur in 'str' /// /// string to search occurences for /// string to search in /// true to ignore case, false otherwise /// the number of occurences of 'value' in 'str', 0 for none public static uint NumberOfSpecStringFoundInString(string value, string str, bool bIgnoreCase = false) { uint nCount = 0; if (!String.IsNullOrEmpty(str)) { int nIndex = 0; StringComparison comparison = bIgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; do { nIndex = str.IndexOf(value, nIndex, comparison); if(nIndex != -1) ++nCount; } while (nIndex != -1); } return nCount; } /// /// Check to see if string starts with a letter /// /// string to check /// true if string starts with a letter public static bool StringStartsWithLetter(string str) { if (!String.IsNullOrEmpty(str)) return char.IsLetter(str[0]); return false; } /// /// Check to see if string ends with a letter /// /// string to check /// true if string ends with a letter public static bool StringEndsWithLetter(string str) { if (!String.IsNullOrEmpty(str)) return char.IsLetter(str[str.Length - 1]); return false; } /// /// Returns the last word of a string (string must end with a letter) /// /// string to go thru /// the last word of a string public static string StringFetchLastWord(string str) { if (!String.IsNullOrEmpty(str) && StringEndsWithLetter(str)) { // Find the last word StringBuilder sb = new StringBuilder(); for (int i = str.Length - 1; i >= 0; --i) { if (char.IsLetter(str[i])) sb.Append(str[i]); else break; } // Reverse the String string strRet = sb.ToString(); #if NET4 sb.Clear(); #else sb = new StringBuilder(); #endif for (int i = strRet.Length - 1; i >=0; --i) sb.Append(strRet[i]); // return the string return sb.ToString(); } return String.Empty; } /// /// Returns the first word of a string (string must start with a letter) /// /// string to go thru /// the first word of a string public static string StringFetchFirstWord(string str) { if (!String.IsNullOrEmpty(str) && StringStartsWithLetter(str)) { // Find the last word StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.Length; ++i) { if (char.IsLetter(str[i])) sb.Append(str[i]); else break; } // return the string return sb.ToString(); } return String.Empty; } /// /// Stripes the last word from the referenced string (string must end with a letter) /// /// last word stripped from string public static void StringStripeLastWord(ref string str) { if (!String.IsNullOrEmpty(str) && StringEndsWithLetter(str)) { // Find the last word int i = 0; for (i = str.Length - 1; i >= 0; --i) { if (!char.IsLetter(str[i])) break; } // Return string without last word str = str.Substring(0, i + 1); } } /// /// Stripes the first word from the referenced string (string must start with a letter) /// /// first word stripped from string public static void StringStripeFirstWord(ref string str) { if (!String.IsNullOrEmpty(str) && StringStartsWithLetter(str)) { // Find the last word int i = 0; for (i = 0; i < str.Length; ++i) { if (!char.IsLetter(str[i])) break; } // Return string without first word str = str.Substring(i); } } } }