Using C# to check if string contains a string in string array
我想使用c检查字符串值是否包含字符串数组中的单词。例如,
1 2 3 4 5 6 7 8 | string stringToCheck ="text1text2text3"; string[] stringArray = {"text1","someothertext", etc... }; if(stringToCheck.contains stringArray) //one of the items? { } |
如何检查"stringtocheck"的字符串值是否在数组中包含单词?
以下是如何:
1 2 | if(stringArray.Any(stringToCheck.Contains)) /* or a bit longer: (stringArray.Any(s => stringToCheck.Contains(s))) */ |
检查
1 | if(stringArray.All(stringToCheck.Contains)) |
你可以这样做:
1 2 3 4 5 6 7 8 9 | string stringToCheck ="text1"; string[] stringArray = {"text1","testtest","test1test2","test2text1" }; foreach (string x in stringArray) { if (stringToCheck.Contains(x)) { // Process... } } |
更新:也许你在寻找更好的解决方案。请参阅下面使用LINQ的@anton gogolev答案。
试试这个:
不需要使用LINQ
1 2 3 4 | if (Array.IndexOf(array, Value) >= 0) { //Your stuff goes here } |
只需使用LINQ方法:
1 | stringArray.Contains(stringToCheck) |
最简单的采样方式。
1 | bool bol=Array.Exists(stringarray,E => E == stringtocheck); |
1 2 3 4 5 6 7 | string strName ="vernie"; string[] strNamesArray = {"roger","vernie","joel" }; if (strNamesArray.Any(x => x == strName)) { // do some action here if true... } |
可能是这样的:
1 2 3 4 5 6 | 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和方法组将是最快和更紧凑的方法。
1 2 3 |
您可以定义自己的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static class Extensions { public static bool Contains(this string source, string value, StringComparison comp) { return source.IndexOf(value, comp) > -1; } public static bool ContainsAny(this string source, IEnumerable<string> values, StringComparison comp = StringComparison.CurrentCulture) { return values.Any(value => source.Contains(value, comp)); } public static bool ContainsAll(this string source, IEnumerable<string> values, StringComparison comp = StringComparison.CurrentCulture) { return values.All(value => source.Contains(value, comp)); } } |
您可以使用以下代码测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void TestExtensions() { string[] searchTerms = {"FOO","BAR" }; string[] documents = { "Hello foo bar", "Hello foo", "Hello" }; foreach (var document in documents) { Console.WriteLine("Testing: {0}", document); Console.WriteLine("ContainsAny: {0}", document.ContainsAny(searchTerms, StringComparison.OrdinalIgnoreCase)); Console.WriteLine("ContainsAll: {0}", document.ContainsAll(searchTerms, StringComparison.OrdinalIgnoreCase)); Console.WriteLine(); } } |
我在控制台应用程序中使用以下内容检查参数
1 | var sendmail = args.Any( o => o.ToLower() =="/sendmail=true"); |
尝试:
1 2 3 4 5 6 | 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)); |
我会使用LINQ,但它仍然可以通过以下方式完成:
1 |
您也可以按照Anton Gogolev建议的方法检查
1 | if(stringArray1.Any(stringArray2.Contains)) |
同样,StringArray1中的所有项都与StringArray2中的所有项匹配:
1 | if(stringArray1.All(stringArray2.Contains)) |
在这里尝试这个示例:检查字段是否包含数组中的任何单词。检查字段(somefield)是否包含数组中的任何单词。
1 2 3 4 5 | String[] val = {"helloword1","orange","grape","pear" }; Expression<Func<Item, bool>> someFieldFilter = i => true; someFieldFilter = i => val.Any(s => i.someField.Contains(s)); |
1 2 3 4 5 6 7 8 9 10 11 | 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的indexof和Theomax的foreach循环的方法来创建这个。(注意:前3个"字符串"行只是一个示例,说明如何创建一个数组并将其转换为正确的格式)。
如果要比较两个数组,它们将以分号分隔,但最后一个值后面不会有一个。如果在数组的字符串形式(即a;b;c变为a;b;c;)中附加一个分号,则无论它在哪个位置,都可以使用"x;"进行匹配:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | bool found = false; string someString ="a-b-c"; string[] arrString = someString.Split('-'); string myStringArray = arrString.ToString() +";"; foreach (string s in otherArray) { if (myStringArray.IndexOf(s +";") != -1) { found = true; break; } } if (found == true) { // .... } |
试试这个
1 2 3 4 | string stringToCheck ="text1text2text3"; string[] stringArray = new string[] {"text1" }; var t = lines.ToList().Find(c => c.Contains(stringToCheck)); |
它将返回您要查找的文本的第一个匹配项所在的行。
对于我来说,上述答案不起作用。我正在检查数组中的字符串并将其分配给布尔值。我修改了@anton gogolev的答案,去掉了
1 | bool = stringArray.Contains(stringToCheck); |
如果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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; } } |
下面是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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; } |
如果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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; } |
简单的解决方案,不需要Linq Any
string.join(",",array).contains(value+",");
1 | int result = Array.BinarySearch(list.ToArray(), typedString, StringComparer.OrdinalIgnoreCase); |
试试这个,不需要循环……
1 2 3 4 5 6 | string stringToCheck ="text1"; List<string> stringList = new List<string>() {"text1","someothertext","etc.." }; if (stringList.Exists(o => stringToCheck.Contains(o))) { } |
若要完成以上答案,请使用以下选项进行忽略检查:
1 | stringArray.Any(s => stringToCheck.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) > -1) |
1 2 3 | string [] lines = {"text1","text2","etc"}; bool bFound = lines.Any(x => x =="Your string to be searched"); |
如果搜索的字符串与数组"lines"的任何元素匹配,则bfund设置为true。
演示了三个选项。我更喜欢第三个最简洁的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 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(); } } |
我使用以下代码检查字符串是否包含字符串数组中的任何项:
1 2 3 4 5 6 7 8 9 10 | foreach (string s in stringArray) { if (s !="") { if (stringToCheck.Contains(s)) { Text ="matched"; } } } |