String matching: Difference between revisions

added Fantom example
m (→‎{{header|D}}: + D & fix header)
(added Fantom example)
Line 282:
println(string1.endsWith(string2))
}</lang>
 
=={{header|Fantom}}==
 
Fantom provides several self-explanatory string-matching methods:
 
* <code>startsWith</code>
* <code>endsWith</code>
* <code>contains</code>
* <code>index</code> (takes an optional index, for the start position)
* <code>indexIgnoreCase</code> (like above, ignoring case for ASCII characters)
* <code>indexr</code> (start search from end of string, with an optional index)
* <code>indexrIgnoreCase</code> (like above, ignoring case for ASCII characters)
 
<lang fantom>
class Main
{
public static Void main ()
{
string := "Fantom Language"
echo ("String is: " + string)
echo ("does string start with 'Fantom'? " + string.startsWith("Fantom"))
echo ("does string start with 'Language'? " + string.startsWith("Language"))
echo ("does string contain 'age'? " + string.contains("age"))
echo ("does string contain 'page'? " + string.contains("page"))
echo ("does string end with 'Fantom'? " + string.endsWith("Fantom"))
echo ("does string end with 'Language'? " + string.endsWith("Language"))
 
echo ("Location of 'age' is: " + string.index("age"))
posn := string.index ("an")
echo ("First location of 'an' is: " + posn)
posn = string.index ("an", posn+1)
echo ("Second location of 'an' is: " + posn)
posn = string.index ("an", posn+1)
if (posn == null) echo ("No third location of 'an'")
}
}
</lang>
 
Output:
<pre>
String is: Fantom Language
does string start with 'Fantom'? true
does string start with 'Language'? false
does string contain 'age'? true
does string contain 'page'? false
does string end with 'Fantom'? false
does string end with 'Language'? true
Location of 'age' is: 12
First location of 'an' is: 1
Second location of 'an' is: 8
No third location of 'an'
</pre>
 
=={{header|Forth}}==
342

edits