String matching: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
(Changed formatting. Added "in" test. Moved "find" at the end.)
Line 2,817: Line 2,817:
<lang nim>import strutils
<lang nim>import strutils


var s: string = "The quick brown fox"
let s = "The quick brown fox"
if startsWith(s, "The quick"):
if s.startsWith("The quick"):
echo("Starts with: The quick")
echo "Starts with “The quick”."
if endsWith(s, "brown Fox"):
if s.endsWith("brown Fox"):
echo("Ends with: brown fox")
echo "Ends with “brown fox”."
var pos = find(s, " brown ") # -1 if not found
if s.contains(" brown "):
echo "Contains “ brown ”."
if contains(s, " brown "): # showing the contains() proc, but could use if pos!=-1:
if "quick" in s:
echo('"' & " brown " & '"' & " is located at position: " & $pos)</lang>
echo "Contains “quick”." # Alternate form for "contains".

let pos = find(s, " brown ") # -1 if not found.
if pos >= 0:
echo " brown is located at position: " & $pos</lang>

{{out}}
{{out}}
<pre>Starts with: The quick
<pre>Starts with “The quick”.
Ends with: brown fox
Contains brown ”.
Contains “quick”.
" brown " is located at position: 9</pre>
brown is located at position: 9</pre>


=={{header|Objeck}}==
=={{header|Objeck}}==