Check to see if a String contains Another String


(JIm) #1

I have a device that returns a string with some info. I am trying to see if the string contains a certain substring. It doesn’t always occur at the same place in the string, so doing it by string position won’t work.

I tried string.contains(string) but that gives me an error.

Is there some way I can do this?


(Dan) #2

Try
originalString.indexOf(“stringToSearchFor”)

int indexOf(String str)
Parameters
Str – The string to search for

Return Value

Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.


(JIm) #3

Finally got back around to this. Thank you for your suggestions. As it turned out I had an error in my original statement. The error message was so crptic I wasn’t seeing it. So I corrected that and now the contains works fine.

Thanks again for the suggestion.