0

I have a String that separates Integers "1,4,15,24"

I want to check if this String contains a specific Integer

E.g. Does it contain 5? False. Does it contain 15? True

Is there any one line solution to check for this in Visual Basic? If not, what's the best solution for this?

5
  • Use instr(str,value to be searched) method to check whether the value is present in a string Commented Jun 9, 2016 at 10:13
  • I am aware of InStr but that will find '5' inside the String since there is the value '15'. Sure I can look for ',5' but that fails when 5 is in the beginning Commented Jun 9, 2016 at 10:16
  • Add an extra comma at start and end of the string during search Commented Jun 9, 2016 at 10:18
  • @testus do you need one line solution to this ? Commented Jun 9, 2016 at 10:18
  • Split the string into an array using the comma as a delimiter, then use a loop to test each value - if the value matches then set a boolean to True and exit the loop. Commented Jun 9, 2016 at 10:19

1 Answer 1

3

could be strange initially but if you add the comma at the start and at the end of the string, in this way ",1,4,15,24," then use instr

pos = InStr(",1,4,15,24,5,", ",5,") 

you can find if the number is present, because you can use the comma as a marker to separe all the items. if pos returns a number different from 0 you have found it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.