environment: microsoft visual studio 2008 c#
How do I get the index of a whole word found in a string
string dateStringsToValidate = "birthdatecake||birthdate||other||strings";
string testValue = "birthdate";
var result = dateStringsToValidate.IndexOf(testValue);
It doesn’t have to be the way i did it either, for example, would it be better to use regular expressions or other methods?
Update:
The word is birthdate not birthdatecake. it doesn’t have to retrieve the match but the index should find the right word. i don’t think IndexOf is what i’m looking for then. Sorry for being unclear.
Use regular expressions for this
Learn more about regex options in c# here
Another option, depending on your needs, is to split the string (as I see you have some delimiters). Please note the index returned by the this option is the index by word count, not character count (In this case, 1, as C# has zero based arrays).