My StringHelper class

I've posted a few code snippets and some people noticed that there are references to unresolved methods.I am using a few helper classes. This post describes my StringHelper class.  By the way, I  (in the article ), that using StringBuilder is often less efficient, than simple concatenation.

Related posts:Helper functions to find pattern contaned in string from pattern List 

ProperCase function in C#  

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Threading;

  6. namespace Common

  7. {

  8.         using Microsoft.VisualBasic;

  9.         using System;

  10.         using System.IO;

  11.         using System.Reflection;

  12.         using System.Text;

  13.         using System.Text.RegularExpressions;

  14.         public static class StringHelper

  15.         {

  16.                 // See also Westwind.Utilities.StringUtils     

  17.                 //'See also FxLib Author: Kamal Patel, Rick Hodder

  18.                 //      'Find the first entry of sToFind and returns the string after it

  19.                 //      'See also FxLib StringExtract (and StuffString)

  20.                 // Methods

  21.                 #region "String Functions"

  22.                 public static string LeftBefore(this string str, string sToFind)

  23.                 {

  24.                         return LeftBefore(str, sToFind, false);

  25.                 }

  26.                 //              'if sToFind not found, then the full string should be returned

  27.                 //              'if sToFind is empty, all string should be returned

  28.                 public static string LeftBefore(this string str, string sToFind, bool EmptyIfNotFound)

  29.                 {

  30.                         StringBuilder builder1 = new StringBuilder(str);

  31.                         if (sToFind.Length > 0)

  32.                         {

  33.                                 int num1 = str.IndexOf(sToFind);

  34.                                 if (num1 < 0)

  35.                                 {

  36.                                         if (EmptyIfNotFound == true) return "";

  37.                                         else return str;// 6/7/2005 full string should be returned

  38.                                 }

  39.                                 builder1.Remove(num1, builder1.Length - num1);

  40.                         }

  41.                         return builder1.ToString;

  42.                 }

  43.                 //              'if sToFind not found, then original string should be returned

  44.                 public static string LeftBeforeLast(this string str, string sToFind)

  45.                 {

  46.                         StringBuilder builder1 = new StringBuilder(str);

  47.                         if (sToFind.Length > 0)

  48.                         {

  49.                                 int num1 = str.LastIndexOf(sToFind);

  50.                                 if (num1 < 0)

  51.                                 {

  52.                                         return str;

  53.                                 }

  54.                                 builder1.Remove(num1, builder1.Length - num1);

  55.                         }

  56.                         return builder1.ToString;

  57.                 }

  58.                 public static string LeftBeforeLast(this string str, string sToFind, intFindNumberTimes)//6/1/2006

  59.                 {

  60.                         for (int i = 0; i < FindNumberTimes; i++)

  61.                         {

  62.                                 str = LeftBeforeLast(str, sToFind);

  63.                         }

  64.                         return str;

  65.                 }

  66.                 //              'if sBefore not found, then string from the beginning should be returned

  67.                 //              'if sAfter not found, then string up to the end should be returned

  68.                 public static string MidBetween(this string str, string sBefore, string sAfter)

  69.                 {

  70.                         return MidBetween(str, sBefore, sAfter, false);

  71.                 }

  72.                 public static string MidBetween(this string str, string sBefore, string sAfter, boolEmptyIfNotFound)

  73.                 {

  74.                         string text2 = RightAfter(str, sBefore, EmptyIfNotFound);

  75.                         return LeftBefore(text2, sAfter, EmptyIfNotFound);

  76.                 }

  77.                 ///

  78.                 ///if sToFind not found, then original string should be returned

  79.                 ///if sToFind is empty, all string should be returned

  80.                 ///

  81.                 ///

  82.                 ///

  83.                 public static string RightAfter(this string str, string sToFind)

  84.                 {

  85.                         return RightAfter(str, sToFind, false);

  86.                 }

  87.                 ///

  88.                 ///

  89.                 ///

  90.                 ///

  91.                 ///

  92.                 ///

  93.                 ///

  94.                 public static string RightAfter(this string str, string sToFind, bool EmptyIfNotFound)

  95.                 {

  96.                         StringBuilder builder1 = new StringBuilder(str);

  97.                         int num1 = str.IndexOf(sToFind);

  98.                         if (num1 < 0)

  99.                         {

  100.                                 if (EmptyIfNotFound == true) return "";

  101.                                 else return str;

  102.                         }

  103.                         builder1.Remove(0, num1 + sToFind.Length);

  104.                         return builder1.ToString;

  105.                 }

  106.                 //

  107.                 ///

  108.                 ///if sToFind not found, then original string should be returned

  109.                 /// Otherwise removeBefore

  110.                 ///

  111.                 ///

  112.                 ///

  113.                 ///

  114.                 public static string RemoveBefore(this string str, string sToFind)

  115.                 {

  116.                         int num1 = str.IndexOf(sToFind);

  117.                         if (num1 > 0)

  118.                         {

  119.                                 return str.Remove(0, num1);

  120.                         }

  121.                         else

  122.                         {

  123.                                 return str;

  124.                         }

  125.                 }

  126.                 //'Find the last entry of sToFind and returns the string after it

  127.                 //if sToFind not found, then original string should be returned ' 6/7/2005 change

  128.                 //if sToFind is empty, the original string should be returned

  129.                 public static string RightAfterLast(string str, string sToFind)

  130.                 {

  131.                         StringBuilder builder1 = new StringBuilder(str);

  132.                         int num1 = str.LastIndexOf(sToFind);

  133.                         if (num1 < 0)

  134.                         {

  135.                                 return str;

  136.                         }

  137.                         builder1.Remove(0, num1 + sToFind.Length);

  138.                         return builder1.ToString;

  139.                 }

  140.                 public static string RightAfterLast(string str, char chToFind, intnOccurencesNumber)//6/1/2006

  141.                 { //C++ solutions to find n'th occurence see http://www.codecomments.com/forum272/message731385.html

  142.                         string[] aStr = str.Split(chToFind);

  143.                         StringBuilder sb = new StringBuilder;

  144.                         int nPosInSplit = aStr.Length - nOccurencesNumber;

  145.                         for (int i = nPosInSplit; i < aStr.Length; i++)

  146.                         {

  147.                                 sb.Append(aStr[i]);

  148.                                 sb.Append(chToFind);

  149.                         }

  150.                         sb.Remove(sb.Length - 1, 1);//1 for char

  151.                         return sb.ToString;

  152.                 }

  153.                 //'Removes the start part of the string, if it is matchs, otherwise leave string unchanged

  154.                 public static string TrimStart(this string str, string sStartValue)

  155.                 {

  156.                         if (!String.IsNullOrWhiteSpace(str) && str.StartsWith(sStartValue))

  157.                         {

  158.                                 str = str.Remove(0, sStartValue.Length);

  159.                         }

  160.                         return str;

  161.                 }

  162.                 //              'Removes the end part of the string, if it is matchs, otherwise leave string unchanged

  163.                 public static string TrimEnd(this string str, string sEndValue)

  164.                 {

  165.                         if (str == null) { throw new NullReferenceException("str is null"); }

  166.                         if (str.EndsWith(sEndValue))

  167.                         {

  168.                                 str = str.Remove(str.Length - sEndValue.Length, sEndValue.Length);

  169.                         }

  170.                         return str;

  171.                 }

  172.                 ///

  173.                 /// If lenght of the string is greater than max allowed, remove the end

  174.                 ///

  175.                 ///

  176.                 ///

  177.                 ///

  178.                 public static string TrimLength(this string str, int maxLength)

  179.                 {

  180.                         if (str == null)

  181.                         {

  182.                                 return str;

  183.                         }

  184.                         if (str.Length > maxLength)

  185.                         {

  186.                                 str = str.Remove(maxLength);

  187.                         }

  188.                         return str;

  189.                 }

  190.                 //from http://mennan.kagitkalem.com/CommentView,guid,d8e01e32-49f3-4450-994a-990c4fa0a437.aspx

  191.                 //use the most efficient

  192.                 public static int OccurencesCount(string str, string sToFind)

  193.                 {

  194.                         string copyOrginal = String.Copy(str);

  195.                         int place = 0;

  196.                         int numberOfOccurances = 0;

  197.                         place = copyOrginal.IndexOf(sToFind.ToString);

  198.                         while (place != -1)

  199.                         {

  200.                                 copyOrginal = copyOrginal.Substring(place + 1);

  201.                                 place = copyOrginal.IndexOf(sToFind.ToString);

  202.                                 numberOfOccurances++;

  203.                         }

  204.                         return numberOfOccurances;

  205.                 }

  206.                 //case-incensitive replace from http://www.codeproject.com/cs/samples/fastestcscaseinsstringrep.asp?msg=1835929#xx1835929xx

  207.                 static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType)

  208.                 {

  209.                         if (original == null)

  210.                         {

  211.                                 return null;

  212.                         }

  213.                         if (String.IsNullOrEmpty(pattern))

  214.                         {

  215.                                 return original;

  216.                         }

  217.                         int lenPattern = pattern.Length;

  218.                         int idxPattern = -1;

  219.                         int idxLast = 0;

  220.                         StringBuilder result = new StringBuilder;

  221.                         while (true)

  222.                         {

  223.                                 idxPattern = original.IndexOf(pattern, idxPattern + 1, comparisonType);

  224.                                 if (idxPattern < 0)

  225.                                 {

  226.                                         result.Append(original, idxLast, original.Length - idxLast);

  227.                                         break;

  228.                                 }

  229.                                 result.Append(original, idxLast, idxPattern - idxLast);

  230.                                 result.Append(replacement);

  231.                                 idxLast = idxPattern + lenPattern;

  232.                         }

  233.                         return result.ToString;

  234.                 }

  235.                 ///

  236.                 /// Uses regex '\b' as suggested in //http://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words

  237.                 ///

  238.                 ///

  239.                 ///

  240.                 ///

  241.                 /// e.g. RegexOptions.IgnoreCase

  242.                 ///

  243.                 static public string ReplaceWholeWord(this string original, string wordToFind, stringreplacement, RegexOptions regexOptions = RegexOptions.None)

  244.                 {

  245.                 string  pattern = String.Format(@"\b{0}\b", wordToFind);

  246.                 string ret = Regex.Replace(original, pattern, replacement, regexOptions);

  247.                         return ret;

  248.                 }

  249.                 ///

  250.                 /// Find the last entry of sToFind and replace it with sToReplace string

  251.                 ///

  252.                 ///

  253.                 /// if sToFind not found, then original string should be returned.

  254.                 /// if sToFind is empty, the original string should be returned

  255.                 ///

  256.                 ///

  257.                 public static string ReplaceLast(this string str, string sToFind, string sToReplace)

  258.                 {

  259.                         StringBuilder builder1 = new StringBuilder(str);

  260.                         int num1 = str.LastIndexOf(sToFind);

  261.                         if (num1 < 0)

  262.                         {

  263.                                 return str;

  264.                         }

  265.                         builder1.Replace(sToFind, sToReplace, num1, sToFind.Length);

  266.                         return builder1.ToString;

  267.                 }

  268.                 public static string IfNotEmptyEnsureEndsWith(string str, string sEndValue)

  269.                 {

  270.                         if (String.IsNullOrEmpty(str)) return str; //21/10/2005

  271.                         if (!str.EndsWith(sEndValue))

  272.                         {

  273.                                 str = str + sEndValue;

  274.                         }

  275.                         return str;

  276.                 }

  277.                 public static string EnsureEndsWith(this string str, string sEndValue)

  278.                 {

  279.                         if (!str.EndsWith(sEndValue))

  280.                         {

  281.                                 str = str + sEndValue;

  282.                         }

  283.                         return str;

  284.                 }

  285.                 //converted from http://stackoverflow.com/questions/1250514/find-length-of-initial-segment-matching-mask-on-arrays

  286.                 public static string LongestCommonPrefix(string str1, string str2)

  287.                 {

  288.                         int minLen = Math.Min(str1.Length, str2.Length);

  289.                         for (int i = 0; i < minLen; i++)

  290.                         {

  291.                                 if (str1[i] != str2[i])

  292.                                 {

  293.                                         return str1.Substring(0, i);

  294.                                 }

  295.                         }

  296.                         return str1.Substring(0, minLen);

  297.                 }

  298.                 ///

  299.                 ///  Adds Prefix, if it is not exist in the string, case sensitive

  300.                 ///

  301.                 /// if null, returns prefix

  302.                 /// if null or empty, returns original string

  303.                 ///

  304.                 public static string EnsureStartsWith(this string str, string sPrefix)

  305.                 {

  306.                         if (str == null)

  307.                         { //throw new ArgumentNullException("str");

  308.                                 return sPrefix;

  309.                         }

  310.                         if (!String.IsNullOrEmpty(sPrefix))

  311.                         {

  312.                                 if (!str.StartsWith(sPrefix))

  313.                                 {

  314.                                         str = sPrefix + str;

  315.                                 }

  316.                         }

  317.                         return str;

  318.                 }

  319.                 public static string AppendWithDelimeter(string str, string sToAppend, string delimeter)

  320.                 {

  321.                         if ((!str.EndsWith(delimeter) & !String.IsNullOrEmpty(str)) &!String.IsNullOrEmpty(sToAppend))

  322.                         {

  323.                                 str = str + delimeter;

  324.                         }

  325.                         str = str + sToAppend;

  326.                         return str;

  327.                 }

  328.                 public static string AppendIfNotContains(string str, string sToAppend, string delimeter)

  329.                 {

  330.                         if (!str.Contains(sToAppend))

  331.                         {

  332.                                 str = AppendWithDelimeter(str, sToAppend, delimeter);

  333.                         }

  334.                         return str;

  335.                 }

  336.                 public static string AppendIfNotEmpty(this string str, string prefixBeforeAppend,stringvalueToAppend,string suffixAfterAppend="")

  337.                 {

  338.                         if (!String.IsNullOrEmpty(valueToAppend))

  339.                         {

  340.                                 str += prefixBeforeAppend + valueToAppend + suffixAfterAppend;

  341.                         }

  342.                         return str;

  343.                 }

  344.                 //from http://66.102.7.104/search?q=cache:DSw2bnf\_FlMJ:blogs.msdn.com/brada/archive/2004/02/16/73535.aspx+%22EndsWith+char+%22+string+C%23&hl=en

  345.                 //" internal bool EndsWith(char value) in String class. Why it would be internal? Also, there is no bool StartsWith(char value). "

  346.                 public static bool EndsWith(string str, char value)

  347.                 {

  348.                         int num1 = str.Length;

  349.                         if ((num1 != 0) && (str[num1 - 1] == value))

  350.                         {

  351.                                 return true;

  352.                         }

  353.                         return false;

  354.                 }

  355.                 public static bool StartsWith(string str, char value)

  356.                 {

  357.                         if ((str.Length != 0) && (str[0] == value))

  358.                         {

  359.                                 return true;

  360.                         }

  361.                         return false;

  362.                 }

  363.                 #region Case conversions

  364.                 ///

  365.                 ///

  366.                 ///

  367.                 ///

  368.                 ///

  369.                 ///

  370.                 /// from http://dotnetjunkies.com/WebLog/davetrux/archive/2006/05/22/138692.aspx, http://west-wind.com/weblog/posts/361.aspx

  371.                 ///  http://www.logiclabz.com/c/title-case-proper-case-function-in-net-c.aspx

  372.                 /// use TextInfo.ToTitleCase(mText.ToLower);

  373.                 /// Alternative see From http://aspcode.net/propercase-function-in-c/

  374.                 ///    

  375.                 public static string ToTitleCase(this string input)

  376.                 {

  377.                         input = input.ToLower;

  378.                         return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(input);

  379.                 }

  380.                 public static string ToCamelCase(this string s)

  381.                 {

  382.                         var sb = new StringBuilder;

  383.                         char[] ca = s.ToLower.ToCharArray;

  384.                         for (int i = 0; i < ca.Length; i++)

  385.                         {

  386.                                 char c = ca[i];

  387.                                 if (i == 0 || Char.IsSeparator(ca[i - 1]))

  388.                                 {

  389.                                         c = Char.ToUpper(c);

  390.                                 }

  391.                                 sb.Append(c);

  392.                         }

  393.                         return sb.ToString;

  394.                 }

  395.                 ///

  396.                 /// Convert string from pascal case to human readable string

  397.                 /// pascalCaseExample => Pascal Case Example

  398.                 ///

  399.                 /// The string

  400.                 /// human readable string

  401.                 public static string ToHumanFromPascal(string s)

  402.                 {

  403.                         StringBuilder sb = new StringBuilder;

  404.                         char[] ca = s.ToCharArray;

  405.                         sb.Append(ca[0]);

  406.                         for (int i = 1; i < ca.Length - 1; i++)

  407.                         {

  408.                                 char c = ca[i];

  409.                                 if (Char.IsUpper(c) && (Char.IsLower(ca[i + 1]) || Char.IsLower(ca[i - 1])))

  410.                                 {

  411.                                         sb.Append(" ");

  412.                                 }

  413.                                 sb.Append(c);

  414.                         }

  415.                         sb.Append(ca[ca.Length - 1]);

  416.                         return sb.ToString;

  417.                 }

  418.                 #endregion //Case conversions

  419.                 //Alternatively see http://weblogs.asp.net/sushilasb/archive/2006/08/03/How-to-extract-numbers-from-string.aspx

  420.                 public static string GetStartingNumericFromString(string itmName)

  421.                 {

  422.                         string safeNumericString = "";

  423.                         foreach (char s in itmName)

  424.                         {

  425.                                 if (s.CompareTo('0') < 0 || s.CompareTo('9') > 0)

  426.                                 {

  427.                                         break;

  428.                                 }

  429.                                 safeNumericString += s.ToString;

  430.                         }

  431.                         return safeNumericString;

  432.                 }

  433.                 ///

  434.                 /// method for removing all whitespace from a given string

  435.                 ///

  436.                 /// string to strip

  437.                 ///

  438.                 public static string RemoveAllWhitespace(string str)

  439.                 {

  440.                         Regex reg = new Regex(@"\s*");

  441.                         str = reg.Replace(str, "");

  442.                         return str;

  443.                 }

  444.                 /*From http://bytes.com/topic/c-sharp/answers/253519-using-regex-create-sqls-like-like-function

  445.                  * Ex:

  446. *

  447. * bool isMatch =

  448. * IsSqlLikeMatch("abcdef", "[az]_%[^qz]ef");

  449. *

  450. * should return true.

  451. */

  452.                 ///

  453.                 /// Note that it could be very serious performance hit, if the pattern is started with %.

  454.                 ///

  455.                 ///

  456.                 ///

  457.                 ///

  458.                 public static bool IsSqlLikeMatch(this string input, string pattern)

  459.                 {

  460.                         /* Turn "off" all regular expression related syntax in

  461.                         * the pattern string. */

  462.                         pattern = Regex.Escape(pattern);

  463.                         /* Replace the SQL LIKE wildcard metacharacters with the

  464.                         * equivalent regular expression metacharacters. */

  465.                         pattern = pattern.Replace("%", ".*?").Replace("_", ".");

  466.                         /* The previous call to Regex.Escape actually turned off

  467.                         * too many metacharacters, i.e. those which are recognized by

  468.                         * both the regular expression engine and the SQL LIKE

  469.                         * statement ([...] and [^...]). Those metacharacters have

  470.                         * to be manually unescaped here. */

  471.                         pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");

  472.                         return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase |RegexOptions.Singleline);

  473.                 }

  474.         ///

  475.         /// Determines whether [contains] [the specified source] with string comparison.

  476.         ///

  477.         /// The source.

  478.         /// To check.

  479.         /// The comp.

  480.         ///

  481.         ///   true if [contains] [the specified source]; otherwise, false.

  482.         ///

  483.         public static bool Contains(this string source, string toCheck, StringComparison comp)

  484.         {

  485.             return source.IndexOf(toCheck, comp) >= 0;

  486.         }

  487.                 #endregion //"String Functions"

  488.                 #region "String Array Functions"

  489.                 public static string[] ToLower(string[] sArray)

  490.                 {

  491.                         for (int i = 0; i < sArray.Length; i++)

  492.                         {

  493.                                 sArray[i] = sArray[i].ToLower;

  494.                         }

  495.                         return sArray;

  496.                 }

  497.                 //see also a few methods in http://www.codeproject.com/csharp/StringBuilder\_vs\_String.asp

  498.                 public static string Join(string separator, params string[] list)

  499.                 {

  500.                         return String.Join(separator, list);

  501.                 }

  502.                 #endregion //"String Array Functions"

  503.                 #region "String Brackets Functions"

  504.                 //             

  505.                 ///

  506.                 /// 'StripBrackets checks that starts from sStart and ends with sEnd (case sensitive).

  507.                 ///             'If yes, than removes sStart and sEnd.

  508.                 ///             'Otherwise returns full string unchanges

  509.                 ///             'See also MidBetween

  510.                 ///

  511.                 ///

  512.                 ///

  513.                 ///

  514.                 ///

  515.                 public static string StripBrackets(string str, string sStart, string sEnd)

  516.                 {

  517.                         if (CheckBrackets(str, sStart, sEnd))

  518.                         {

  519.                                 str = str.Substring(sStart.Length, (str.Length - sStart.Length) -sEnd.Length);

  520.                         }

  521.                         return str;

  522.                 }

  523.                 public static bool CheckBrackets(string str, string sStart, string sEnd)

  524.                 {

  525.                         bool flag1 = false;

  526.                         if ((str != null) && (str.StartsWith(sStart) && str.EndsWith(sEnd)))//19/5/ null handling

  527.                         {

  528.                                 flag1 = true;

  529.                         }

  530.                         return flag1;

  531.                 }

  532.                 public static string WrapBrackets(string str, string sStartBracket, string sEndBracket)

  533.                 {

  534.                         StringBuilder builder1 = new StringBuilder(sStartBracket);

  535.                         builder1.Append(str);

  536.                         builder1.Append(sEndBracket);

  537.                         return builder1.ToString;

  538.                 }

  539.                 //    'Concatenates a specified separator String between each element of a specified String array wrapping each element, yielding a single concatenated string

  540.                 public static string JoinWrapBrackets(string[] aStr, string sDelimeter, string sStartBracket,string sEndBracket)

  541.                 {

  542.                         StringBuilder builder1 = new StringBuilder;

  543.                         string[] textArray1 = aStr;

  544.                         for (int num1 = 0; num1 < textArray1.Length; num1++)

  545.                         {

  546.                                 string text2 = textArray1[num1];

  547.                                 builder1.Append(WrapBrackets(text2, sStartBracket, sEndBracket));

  548.                                 builder1.Append(sDelimeter);

  549.                         }

  550.                         return TrimEnd(builder1.ToString, sDelimeter);

  551.                 }

  552.                 ///

  553.                 ///

  554.                 ///

  555.                 ///

  556.                 ///

  557.                 ///

  558.                 ///

  559.                 ///

  560.                 ///

  561.                 ///     // mask XXXXX4488

  562.                 ///requestAsString  = requestAsString.ReplaceBetweenTags("", "", CreditCard.MaskedCardNumber);

  563.                 ///mask cvv

  564.                 ///requestAsString = requestAsString.ReplaceBetweenTags("CC::VerificationCode", "", cvv=>"XXX");

  565.                 ///

  566.                 public static string ReplaceBetweenTags(this string thisString, string openTag, stringcloseTag, Func<string, string> transform)

  567.                 {

  568.                         //See also http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex

  569.                         string sRet = thisString;

  570.                         string between = thisString.MidBetween(openTag, closeTag, true);

  571.                         if (!String.IsNullOrEmpty(between))

  572.                                 sRet=thisString.Replace(openTag + between + closeTag, openTag +transform(between) + closeTag);

  573.                         return sRet;

  574.                 }

  575.                 public static string ReplaceBetweenTags(this string thisString, string openTag, stringcloseTag, string newValue)

  576.                 {

  577.                         //See also http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex

  578.                         string sRet = thisString;

  579.                         string between = thisString.MidBetween(openTag, closeTag, true);

  580.                         if (!String.IsNullOrEmpty(between))

  581.                                 sRet = thisString.Replace(openTag + between + closeTag, openTag + newValue +closeTag);

  582.                         return sRet;

  583.                 }

  584.                 //              ' Quote the arguments, in case they have a space in them.

  585.                 public static string QuotePath(string sPath)

  586.                 {

  587.                         return ("\"" + sPath + "\"");

  588.                 }

  589.                 public static string DblQuoted(string sWord)

  590.                 {

  591.                         sWord = Strings.Replace(sWord, "\"", "\"\"", 1, -1, CompareMethod.Binary);

  592.                         return ("\"" + sWord + "\"");

  593.                 }

  594.                 #endregion //"String Brackets Functions"

  595.                 ///

  596.                 /// Returns true, if  string contains any of substring from the list (case insensitive)

  597.                 /// See similar (with SqlLikeMatch support) in ResponseMessagePatternsCache

  598.                 ///

  599.                 ///

  600.                 public static bool IsStringContainsAnyFromList(this string stringToSearch,List<String>stringsToFind)

  601.                 {

  602.                         //TODO: create overloads with exact match  or case sencitive

  603.                         if (stringsToFind.IsNullOrEmpty)

  604.                         { return false; }

  605.                         else

  606.                         {

  607.                                 stringToSearch = stringToSearch.ToUpper;

  608.                                 return stringsToFind.Any(pattern =>stringToSearch.Contains(pattern.ToUpper));

  609.                         }

  610.                 }

  611.         }

  612. }

v

This article is part of the GWB Archives. Original Author: Michael Freidgeim

New on Geeks with Blogs