String search: Difference between revisions
Jump to navigation
Jump to search
(imported material) |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
=== Problem === |
=== Problem === |
||
Search a sub |
Search a sub-string in some String. |
||
=== Solution === |
=== Solution === |
||
Line 10: | Line 10: | ||
* <code>findString: subString startingAt: anIndex caseSensitive: aBoolean</code> : case sensitive version. |
* <code>findString: subString startingAt: anIndex caseSensitive: aBoolean</code> : case sensitive version. |
||
For example: |
For example:<syntaxhighlight lang="smalltalk"> |
||
'foobarbaz' findString: 'foo'. "=> 1" |
|||
'foobarbaz' findString: 'bar'. "=> 4" |
|||
'foobarbaz' findString: 'baz'. "=> 7" |
|||
</syntaxhighlight> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</syntaxhighlight> |
Latest revision as of 19:59, 12 May 2025
Problem
Search a sub-string in 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"