使用C#检查string是否包含string数组中的string

我想用C#来检查一个string值是否包含string数组中的一个字。 例如,

string stringToCheck = "text1text2text3"; string[] stringArray = { "text1", "someothertext", etc... }; if(stringToCheck.contains stringArray) //one of the items? { } 

如何检查“stringToCheck”的string值是否包含数组中的单词?

这里是你如何做到这一点:

 string stringToCheck = "text1"; string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" }; foreach (string x in stringArray) { if (stringToCheck.Contains(x)) { // Process... } } 

更新:可能是你正在寻找一个更好的解决scheme..参考@Anton Gogolev的答案下面,使用LINQ。

就是这样:

 if(stringArray.Any(stringToCheck.Contains)) /* or a bit longer: (stringArray.Any(s => stringToCheck.Contains(s))) */ 

这将检查stringToCheck包含stringArray中的任何一个子string。 如果要确保它包含所有子string,请将Any更改为All

 if(stringArray.All(stringToCheck.Contains)) 

尝试这个:

不需要使用LINQ

 if (Array.IndexOf(array, Value) >= 0) { //Your stuff goes here } 

只要使用linq方法:

 stringArray.Contains(stringToCheck) 

(对不起,现有答案不能添加评论,因为我的声望<50)

最简单和样本的方式。

  bool bol=Array.Exists(stringarray,E => E == stringtocheck); 

也许这样的东西:

 string stringToCheck = "text1text2text3"; string[] stringArray = new string[] { "text1" }; if (Array.Exists<string>(stringArray, (Predicate<string>)delegate(string s) { return stringToCheck.IndexOf(s, StringComparison.OrdinalIgnoreCase) > -1; })) { Console.WriteLine("Found!"); } 

使用Linq和方法组将是最快和最紧凑的方法。

 var arrayA = new[] {"element1", "element2"}; var arrayB = new[] {"element2", "element3"}; if (arrayB.Any(arrayA.Contains)) return true; 
 string strName = "vernie"; string[] strNamesArray = { "roger", "vernie", "joel" }; if (strNamesArray.Any(x => x == strName)) { // do some action here if true... } 

我会使用Linq,但它仍然可以通过:

 new[] {"text1", "text2", "etc"}.Contains(ItemToFind); 

尝试:

 String[] val = { "helloword1", "orange", "grape", "pear" }; String sep = ""; string stringToCheck = "word1"; bool match = String.Join(sep,val).Contains(stringToCheck); bool anothermatch = val.Any(s => s.Contains(stringToCheck)); 

您也可以像Anton Gogolevbuild议的那样检查stringArray1中的任何项是否与stringArray1中的任何项匹配:

 if(stringArray1.Any(stringArray2.Contains)) 

同样,stringArray1中的所有项 匹配stringArray2中的所有项目

 if(stringArray1.All(stringArray2.Contains)) 

我在控制台应用程序中使用以下内容来检查参数

 var sendmail = args.Any( o => o.ToLower() == "/sendmail=true"); 

试试这个,这里的例子:检查字段是否包含数组中的任何单词。 检查字段(someField)是否包含数组中的任何单词。

 String[] val = { "helloword1", "orange", "grape", "pear" }; Expression<Func<Item, bool>> someFieldFilter = i => true; someFieldFilter = i => val.Any(s => i.someField.Contains(s)); 
 public bool ContainAnyOf(string word, string[] array) { for (int i = 0; i < array.Length; i++) { if (word.Contains(array[i])) { return true; } } return false; } 

我用Maitrey684和Theomax的foreach循环的IndexOf类似的方法来创build这个。 (注意:前3个“string”行只是一个如何创build数组并将其转换为正确格式的示例)。

如果你想比较2个数组,他们将被分号分隔,但最后一个值不会有一个。 如果在数组的stringforms(即a; b; c变成a; b; c;)后附加了一个分号,可以使用“x”匹配。 不pipe在什么位置:

 bool found = false; string someString = "abc"; string[] arrString = someString.Split('-'); string myStringArray = arrString.ToString() + ";"; foreach (string s in otherArray) { if (myStringArray.IndexOf(s + ";") != -1) { found = true; break; } } if (found == true) { // .... } 
 string [] lines = {"text1", "text2", "etc"}; bool bFound = lines.Any(x => x == "Your string to be searched"); 

如果searchstring与数组“行”的任何元素匹配,则bFound设置为true。

尝试这个

 string stringToCheck = "text1text2text3"; string[] stringArray = new string[] { "text1" }; var t = lines.ToList().Find(c => c.Contains(stringToCheck)); 

它将返回您正在寻找的文本的第一个发生的行。

如果stringArray包含大量不同长度的string,请考虑使用Trie来存储和searchstring数组。

 public static class Extensions { public static bool ContainsAny(this string stringToCheck, IEnumerable<string> stringArray) { Trie trie = new Trie(stringArray); for (int i = 0; i < stringToCheck.Length; ++i) { if (trie.MatchesPrefix(stringToCheck.Substring(i))) { return true; } } return false; } } 

这里是Trie类的实现

 public class Trie { public Trie(IEnumerable<string> words) { Root = new Node { Letter = '\0' }; foreach (string word in words) { this.Insert(word); } } public bool MatchesPrefix(string sentence) { if (sentence == null) { return false; } Node current = Root; foreach (char letter in sentence) { if (current.Links.ContainsKey(letter)) { current = current.Links[letter]; if (current.IsWord) { return true; } } else { return false; } } return false; } private void Insert(string word) { if (word == null) { throw new ArgumentNullException(); } Node current = Root; foreach (char letter in word) { if (current.Links.ContainsKey(letter)) { current = current.Links[letter]; } else { Node newNode = new Node { Letter = letter }; current.Links.Add(letter, newNode); current = newNode; } } current.IsWord = true; } private class Node { public char Letter; public SortedList<char, Node> Links = new SortedList<char, Node>(); public bool IsWord; } private Node Root; } 

如果stringArray中的所有string具有相同的长度,那么使用HashSet代替Trie会更好

 public static bool ContainsAny(this string stringToCheck, IEnumerable<string> stringArray) { int stringLength = stringArray.First().Length; HashSet<string> stringSet = new HashSet<string>(stringArray); for (int i = 0; i < stringToCheck.Length - stringLength; ++i) { if (stringSet.Contains(stringToCheck.Substring(i, stringLength))) { return true; } } return false; } 

简单的解决scheme,不需要LINQ任何

String.Join(“,”,array).Contains(Value +“,”);

 int result = Array.BinarySearch(list.ToArray(), typedString, StringComparer.OrdinalIgnoreCase); 

展示了三个选项。 我更喜欢find最简洁的第三个。

 class Program { static void Main(string[] args) { string req = "PUT"; if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) { Console.WriteLine("one.1.A"); // IS TRUE } req = "XPUT"; if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) { Console.WriteLine("one.1.B"); // IS TRUE } req = "PUTX"; if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) { Console.WriteLine("one.1.C"); // IS TRUE } req = "UT"; if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) { Console.WriteLine("one.1.D"); // false } req = "PU"; if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) { Console.WriteLine("one.1.E"); // false } req = "POST"; if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) { Console.WriteLine("two.1.A"); // IS TRUE } req = "ASD"; if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) { Console.WriteLine("three.1.A"); // false } Console.WriteLine("-----"); req = "PUT"; if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) { Console.WriteLine("one.2.A"); // IS TRUE } req = "XPUT"; if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) { Console.WriteLine("one.2.B"); // false } req = "PUTX"; if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) { Console.WriteLine("one.2.C"); // false } req = "UT"; if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) { Console.WriteLine("one.2.D"); // false } req = "PU"; if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) { Console.WriteLine("one.2.E"); // false } req = "POST"; if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) { Console.WriteLine("two.2.A"); // IS TRUE } req = "ASD"; if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) { Console.WriteLine("three.2.A"); // false } Console.WriteLine("-----"); req = "PUT"; if ((new string[] {"PUT", "POST"}.Contains(req))) { Console.WriteLine("one.3.A"); // IS TRUE } req = "XPUT"; if ((new string[] {"PUT", "POST"}.Contains(req))) { Console.WriteLine("one.3.B"); // false } req = "PUTX"; if ((new string[] {"PUT", "POST"}.Contains(req))) { Console.WriteLine("one.3.C"); // false } req = "UT"; if ((new string[] {"PUT", "POST"}.Contains(req))) { Console.WriteLine("one.3.D"); // false } req = "PU"; if ((new string[] {"PUT", "POST"}.Contains(req))) { Console.WriteLine("one.3.E"); // false } req = "POST"; if ((new string[] {"PUT", "POST"}.Contains(req))) { Console.WriteLine("two.3.A"); // IS TRUE } req = "ASD"; if ((new string[] {"PUT", "POST"}.Contains(req))) { Console.WriteLine("three.3.A"); // false } Console.ReadKey(); } } 

试试这个,不需要循环..

 string stringToCheck = "text1"; List<string> stringList = new List<string>() { "text1", "someothertext", "etc.." }; if (stringList.Exists(o => stringToCheck.Contains(o))) { } 

我使用下面的代码来检查string是否包含string数组中的任何项:

 foreach (string s in stringArray) { if (s != "") { if (stringToCheck.Contains(s)) { Text = "matched"; } } }