Home
Manage Your Code
Snippet: isIn (C#)
Title: isIn Language: C#
Description: Search a delimited string for a substring. Views: 171
Author: Rich Krueger Date Added: 10/28/2007
Copy Code  
1  public static bool isIn(string searchString, string delimitedString)
2  {
3    return isIn(searchString, delimitedString, ",", false);
4  }
5
6  public static bool isIn(string searchString, string delimitedString, string delimiter)
7  {
8    return isIn(searchString, delimitedString, delimiter, false);
9  }
10
11  public static bool isIn(string searchString, string delimitedString, bool caseSensitive)
12  {
13    return isIn(searchString, delimitedString, ",", caseSensitive);
14  }
15
16  public static bool isIn(string searchString, string delimitedString, string delimeter, bool caseSensitive)
17  {
18    try
19    {
20      if (delimeter == "" || delimeter == null)
21        delimeter = ",";
22      if (searchString == "" || searchString == null || delimitedString == "" || delimitedString == null)
23        return false;
24      else
25      {
26        bool ret = false;
27        string[] arr = delimitedString.Split(delimeter.ToCharArray());
28        foreach (string s in arr)
29        {
30          if (caseSensitive)
31          {
32            if (searchString == s) { ret = true; }
33          }
34          else
35          {
36            if (searchString.ToLower() == s.ToLower()) { ret = true; }
37          }
38        }
39        return ret;
40      }
41
42    }
43    catch { return false; }
44  }
45