I'm trying to get a word from a bunch of randomtyped characters or words, for example, I want check if the word dog
exists in the following string:
string animal = "MyNewdogIsVeryPlayful";
How would I manage this?
I'm trying to get a word from a bunch of randomtyped characters or words, for example, I want check if the word dog
exists in the following string:
string animal = "MyNewdogIsVeryPlayful";
How would I manage this?
How about:
string animal = "MyNewdogIsVeryPlayful";
bool containsDog = animal.ToUpperInvariant().Contains("DOG");
This will work regardless of the casing of the original string.
If the string is camel-cased, and you want to split the string up to get the constituent words, have a look at this answer here.
string animal = "MyNewdogIsVeryPlayful";
bool isDogContained = animal.Contains("dog");
Assuming you want to check for different animals, you could create an extension method (place it in a separate static class):
public static bool Contains(this string listOfAnimals, string animalToSearchFor){
return listOfAnimals.Contains(animalToSearchFor);
}
That will let you do e.g.:
bool doesItContainLion = "MyNewdogIsVeryPlayful".Contains("lion");