-2

So I'm wondering what would be the correct approach to checking a response,

Example -

response = "43";

then if I use

if(response.Contains("4")) {
    //do code
}

if(response.Contains("3")) {
    //do code
}

both would be equally true, therefore both executing a function, however I want it to be equal to start and end of string, so I'm using -

if(response.Equals("3")) {
   //do code
} 

therefore the above function would only execute if it WAS EQUAL TO 3 not containing 3? ( Please correct me if I'm wrong )

However for one statement I want to check for multiple strings/integers. So it would be more efficient for my if statement to check each item in a list, rather than repeat

response.Equals(".") && response.Equals(".") etc etc

how could do I do this?

So check if response is equal to any item in

 List<string> mylist = new List<string>(new string[] { "1", "2", "3" });

preferably without a for loop..

InfoSec
  • 29
  • 6
  • why don't you loop the list and check each element? – Harry Mar 26 '19 at 21:27
  • if (mylist.Contains(response)).... ? – trix Mar 26 '19 at 21:28
  • 1
    why isn't any of this valid C#? – Keith Nicholas Mar 26 '19 at 21:29
  • 1
    Please ensure your code compiles AND reflects the question. `43` is not a string, you cannot call `Contains` on it – Camilo Terevinto Mar 26 '19 at 21:29
  • Also, please fix your title, move the C# to a tag, and put a question in the title – Keith Nicholas Mar 26 '19 at 21:30
  • @KeithNicholas how isn't any of this valid C#? unless you're referring to the casing.. and therefore ruling it out as invalid c#? – InfoSec Mar 26 '19 at 21:31
  • it should all be valid syntax, questions should be high quality with good example code – Keith Nicholas Mar 26 '19 at 21:31
  • Well I don't really know how I could example this much better in a simplistic way? However I will agree to the syntax, I was just producing my examples quickly so dismissed casing.. but as long as you understand the references then what is the issue? – InfoSec Mar 26 '19 at 21:35
  • the issue, and since you are new to SO, is that this isn't a forum, it pays to get familiar with SO guidelines, as you will get downvoted a lot. This is like the wikipedia of coding questions, your question is like the main wikipedia article describing the question, it should be reasonablly high quality capturing the essential question. – Keith Nicholas Mar 26 '19 at 21:38
  • it's still not valid syntax.... – Keith Nicholas Mar 26 '19 at 21:38
  • Maybe check this link out : https://stackoverflow.com/questions/829174/is-there-an-easy-way-to-turn-an-int-into-an-array-of-ints-of-each-digit – edm2282 Mar 26 '19 at 21:40
  • This question is still pretty unclear... a response of "43" will _not_ match any of the items in that `{ "1", "2", "3" }` array. And "without a for loop" is a pretty bizarre requirement. – Nyerguds Mar 27 '19 at 09:35

1 Answers1

-1

You'l want to use mylist.Contains(response) to check for this. List.Contains checks for exact matches only, so it will avoid your original issue of having two blocks execute.

Adam Wells
  • 545
  • 1
  • 5
  • 15
  • Thank you Adam :), didn't think to essentially reverse the check lol. – InfoSec Mar 26 '19 at 21:32
  • `.Contains` still obviously does a loop internally. Not sure why you would want to avoid `for` loops; they are pretty much always more efficient than the methods offered by the framework, especially on generic types. – Nyerguds Mar 27 '19 at 09:38