I want to use C# to check if a string value contains a word in a string array. For example,

string stringToCheck = "text1text2text3";

string[] stringArray = ("text1", etc... )

if(stringToCheck.contains stringArray( //one of the items?
{

}

How can I check if the string value for 'stringToCheck' contains a word in the string array?

Thanks,

share|improve this question
feedback

7 Answers

up vote 9 down vote accepted

here is how you can do it:

string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
    if (x.Contains(stringToCheck))
    {
        // Process...
    }
}
share|improve this answer
Thanks, I modified your code to: if (stringToCheck.Contains(s)) and it worked. – Theomax May 26 '10 at 13:29
1  
I did if (stringArray.Contains(stringToCheck)) and it works great, thanks. – Tamara JQ Oct 19 '11 at 14:18
3  
Don't use this answer use LINQ instead – AlexC Mar 23 '12 at 11:19
1  
Little note to people who do not see Contains method on the string array: Check if you have a "using System.Linq;" namespace in your codefile :) – mishrsud Apr 5 '12 at 10:33
feedback

Using Linq and method group would be the quickest and more compact way of doing this.

        var arrayA = new[] {"element1", "element2"};
        var arrayB = new[] {"element2", "element3"};
        if (arrayB.Any(arrayA.Contains)) return true;
share|improve this answer
feedback

I used the following code to check if the string contained any of the items in the string array:

foreach (string s in stringArray)
{
    if (s != "")
    {
        if (stringToCheck.Contains(s))
        {
            Text = "matched";
        }
    }
}
share|improve this answer
This sets Text = "matched" as many times as stringToCheck contains substrings of stringArray. You may want to put a break or return after the assignment. – Dour High Arch May 26 '10 at 13:55
feedback

try this, here the example : To check if the field contains any of the words in the array. To check if the field(someField) contains any of the words in the array.

String[] val = { "helloword1", "orange", "grape", "pear" };   

Expression<Func<Item, bool>> someFieldFilter = i => true;

someFieldFilter = i => val.Any(s => i.someField.Contains(s));
share|improve this answer
feedback

try:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            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));

        }
    }
}
share|improve this answer
feedback

Something like this perhaps:

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!");
}
share|improve this answer
feedback

Here's how:

if(stringArray.Any(s => stringToCheck.Contains(s)))
/* or even shorter: stringArray.Any(stringToCheck.Contains)) */

This checks if stringToCheck contains any one of substrings from stringArray. If you want to ensure that it contains all the substrings, change Any to All:

if(stringArray.All(s => stringToCheck.Contains(s)))
share|improve this answer
20  
Note to self: linq is amazing, linq is amazing, linq is amazing! Gotta' start using linq. – Fredrik Johansson May 26 '10 at 11:41
Thanks for your reply, but .all and .any don't seem to be recognised methods? – Theomax May 26 '10 at 13:00
15  
No offence to Abdel, but this ought to be the selected answer. – lazo Dec 14 '10 at 0:02
1  
if you are using framework 2.0... then you can't use linq – Spooks Jun 13 '12 at 23:44
1  
@Spooks Linq To Objects (which is used in the answer's string-check) can be used via LinqBridge on .NET 2.0 albahari.com/nutshell/linqbridge.aspx – Warappa Aug 24 '12 at 20:12
show 2 more comments
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.