1
4

5 回答 5

3

you can use the string.Contains method

bool employeeExistsInString = string.Contains(yourString);

However would have been nice if you would have at least googled it first.

于 2012-09-18T07:43:00.423 回答
2

Use string.Contains

if(str.Contains("EMP4"))

EDIT: based on your comment.

Since you said in your question that your values will be comma separated. You may try the following.

string str = "EMP10, EMP44, EMP5"; //your sample string. 
string[] tempArray = str.Split(',');
if (tempArray.Contains("EMP4")) //This will result in Not Found
{
    Console.WriteLine("Found");
}
if (str.Contains("EMP4")) //But a simple string.Contains will result in matching 
                          //it to EMP44
{
    Console.WriteLine("here");
}
于 2012-09-18T07:42:17.853 回答
2

If you start with the following:

var text = "EMP1$0,EMP2$1,EMP3 $1, Emp4$ 0, emp44$1";

Then you can perform this query to extract the "EMP" codes:

var query =
    from x in text.Split(',').Select(_ => _.Trim())
    let y = x.Split('$').Select(_ => _.Trim())
    where y.Any()
    select y.First();

This would give the following result:

EMP query result

It would now be a simple matter of checking if any of the returned values match.

var found = query.Any(x => x.ToUpperInvariant() == "EMP4");

Does this do what you need?

于 2012-09-18T08:09:33.357 回答
0

Create a list of the csv and use contains on the list

于 2012-09-18T07:43:36.417 回答
0

You could use a regex

Regex r = new Regex("EMP4[^\\d]"); 
Match m = r.Match(yourString); 
if (m.Success) 
{
    // your code
}

This searches any EMP4 string not followed by another digit

于 2012-09-18T07:45:48.587 回答