String matching: Difference between revisions

→‎Tcl: Added implementation
(→‎{{header|Objective-C}}: we only need one objective c section)
(→‎Tcl: Added implementation)
Line 91:
loc = "abab".find("ab",loc+1) #returns 2
</lang>
 
=={{header|Tcl}}==
<lang tcl>set isPrefix [string equal -length [string length $needle] $haystack $needle]
set isContained [expr {[string first $needle $haystack] >= 0}]
set isSuffix [string equal $needle [string range $haystack end-[expr {[string length $needle]-1}] end]]</lang>
 
Of course, in the cases where the needle is a glob-safe string (i.e., doesn't have any of the characters “<tt>*?[</tt>” in), this can be written far more conveniently:
<lang tcl>set isPrefix [string match $needle* $haystack]
set isContained [string match *$needle* $haystack]
set isSuffix [string match *$needle $haystack]</lang>
Anonymous user