-1

I have a list arrString that contains string values. I need to check a string lookupvalue.Longname whether it exists in the list using for loop in C#.

 for(int i = 0 ;i < ResoCustomFields.LongnameNotToBeTaken.Count ;i++)
 {
    string myStringList = ResoCustomFields.LongnameNotToBeTaken[i].ToString();
    var arrString = myStringList.Trim('(',')').Split(',').ToList();
    if(arrString.Contains(resoField))
    {
    // if(!arrString[i].Any(str=>str.ToString().Contains(lookupValue.LongName)))
    // if(lookupValue.LongName.Contains(arrString.ToString()))
    //(!arrString.Any(str => str.Equals(lookupValue.LongName)))
    // if(!arrString.Equals(lookupValue.LongName))                                                      
    {
    }
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Sree
  • 1
  • 4
  • 1
    Possible duplicate of [Check if list contains element that contains a string and get that element](http://stackoverflow.com/questions/18767302/check-if-list-contains-element-that-contains-a-string-and-get-that-element) – Smartis has left SO again Apr 11 '17 at 06:57
  • `if (ResoCustomFields.LongnameNotToBeTaken.Any(line => line.Trim('(',')').Split(',').Any(item => item == resoField))) {...}` – Dmitry Bychenko Apr 11 '17 at 06:59
  • i tried arrString[i].Contains(lookupValue.LongName)..still its not working – Sree Apr 11 '17 at 07:01
  • i have already done trim and split.. – Sree Apr 11 '17 at 07:05
  • Can you give a small example of arrString and resoField? – BugFinder Apr 11 '17 at 07:05
  • Why do you need to check the string after split? Before split only you can see if string contains the other string. – Biswabid Apr 11 '17 at 07:06
  • You could convert the list to an array and use `if(Array.IndexOf(myArray, myString) != -1) // if true, then array contains string`. – JMadelaine Apr 11 '17 at 07:06
  • _"using for loop"_ -- is that really an implementation requirement? Or did you write that only because you can conceive of no other approach than to use a `for` loop? It matters greatly, because it determines what would or would not be an acceptable answer here. Note also that, without a good [mcve] illustrating your question and a precise problem statement, it's impossible to know exactly what you're asking. – Peter Duniho Apr 11 '17 at 07:39
  • split was used as the list contained opening and closing braces when converted string to list – Sree Apr 11 '17 at 09:06

1 Answers1

0

You can try the following code :

int pos = Array.IndexOf(arrString, lookupValue.LongName);
if (pos > -1)
{
//// DO YOUR STUF
}

Following is the reference:

Checking if a string array contains a value, and if so, getting its position
Community
  • 1
  • 1
Biswabid
  • 1,378
  • 11
  • 26