String matching: Difference between revisions
Content added Content deleted
m (Added bc and dc to list of omissions) |
(Updated D entry) |
||
Line 607: | Line 607: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d> |
<lang d>void main() { |
||
import std.stdio; |
|||
import std.algorithm: startsWith, endsWith, find, countUntil; |
import std.algorithm: startsWith, endsWith, find, countUntil; |
||
"abcd".startsWith("ab").writeln; // true |
|||
void main() { |
|||
"abcd".endsWith("zn").writeln; // false |
|||
"abab".find("bb").writeln; // empty array (no match) |
|||
"abcd".find("bc").writeln; // "bcd" (substring start |
|||
writeln("abcd".find("bc")); // "bcd" (substring start |
|||
// at match) |
// at match) |
||
"abab".countUntil("bb").writeln; // -1 (no match) |
|||
"abab".countUntil("ba").writeln; // 1 (index of 1st match) |
|||
// std.algorithm.startsWith also works on arrays and ranges: |
// std.algorithm.startsWith also works on arrays and ranges: |
||
[1, 2, 3].countUntil(3).writeln; // 2 |
|||
[1, 2, 3].countUntil([2, 3]).writeln; // 1 |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |