String search
Jump to navigation
Jump to search
Problem
Search a sub string of some String.
Solution
Use the String>>findString:
methods variations:
findString: subString
: Answers the index ofsubString
within the receiver, starting at start. If the receiver does not containsubString
, answers 0.findString: subString startingAt: anIndex
: Same asfindString
, but starting at index.findString: subString startingAt: anIndex caseSensitive: aBoolean
: case sensitive version.
For example:
'foobarbaz' findString: 'foo'. "=> 1" 'foobarbaz' findString: 'bar'. "=> 4" 'foobarbaz' findString: 'baz'. "=> 7"
And returns 0 if substring is not found:
'foobarbaz' findString: 'hello' "=> 0"