-3

VB.net

Let's consider that we have 6 words: girl, boy, cat, dog, fish, whale

I want to be able to search for each of those words optionally with just a few lines, maybe just an If statement. However, I want to also make it possible that any of these words can show up in any order. I want to be able to recognise any of these words, if they come up in a string, I'd also like to have them accepted no matter how many times they come up.

In English I would write it like this:

If the string = girl and/or boy and/or cat and/or dog and/or fish and/or whale

Of course I also want to make sure that these words can turn up any amount of times.

I realize this is not how And and Or work in VB.net

  • 1
    A string cannot be = "girl" **and** "boy" at the same time. You mean the string contains both? – Tim Schmelter May 23 '18 at 15:57
  • Yes. I want to be able to include both strings, at least one of these, but both are optional. This means i could put: girlboy or boygirl, girlgirl or boyboy (or really any number of this), boy, or girl, but not a string containing nothing. – Joseph Bailey-Wood May 23 '18 at 16:01
  • Please update your post with what you have tried, `I want to be able` is not a question, we help when user's can show what they have tried and where they are stuck, not do it for them. – Trevor May 23 '18 at 16:25
  • Excuse me but I have looked it up. I'd never heard of .contains and I've only just started learning with VB.Net. Don't get angry at people because they lack the knowledge of the field. And before you say I didn't look up, I checked many times and was mostly wondering about how I would do it without accepting extra text. If you know how to do that then please do share. I'd suggest that you don't start calling people "appalling" and claiming they haven't the least decency to search, which any person with a brain is capable of doing. In this case I didn't know how to word what I was looking for. – Joseph Bailey-Wood May 23 '18 at 20:18

2 Answers2

2

I guess you want to check if any of these words are contained:

Dim words = { "girl", "boy", "cat", "dog", "fish", "whale" }
Dim containedWords = words.Where(Function(w) text.Contains(w))

If containedWords.Any() Then

End If

If you want it to be case-insensitive, so accept also "Boy":

Dim containedWords = words.
  Where(Function(w) text.IndexOf(w, StringComparison.CurrentCultureIgnoreCase) >= 0)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
-1

im not familiar with VB.net but i can help you with some logic.

create a boolean value to act as a flag

boolean proceed;
if(file contains first string){
    proceed = true;
}
if(file contains second string){
    proceed = true;
}
if(proceed){
    //do something
}

hope this helps

vmp
  • 271
  • 1
  • 3
  • 13