Jump to content

String matching: Difference between revisions

Updated D entry
m (Added bc and dc to list of omissions)
(Updated D entry)
Line 607:
 
=={{header|D}}==
<lang d>importvoid std.stdio:main() writeln;{
import std.stdio;
import std.algorithm: startsWith, endsWith, find, countUntil;
 
"abcd".startsWith("ab").writeln; // true
void main() {
writeln("abcd".startsWithendsWith("abzn")).writeln; // truefalse
writeln("abcdabab".endsWithfind("znbb")).writeln; // falseempty array (no match)
writeln("abababcd".find("bbbc")).writeln; // empty array"bcd" (nosubstring match)start
writeln("abcd".find("bc")); // "bcd" (substring start
// at match)
writeln("abab".countUntil("bb")).writeln; // -1 (no match)
writeln("abab".countUntil("ba")).writeln; // 1 (index of 1st match)
 
// std.algorithm.startsWith also works on arrays and ranges:
writeln([1, 2, 3].countUntil(3)).writeln; // 2
writeln([1, 2, 3].countUntil([2, 3])).writeln; // 1
}</lang>
{{out}}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.