Count occurrences of a substring: Difference between revisions
Content added Content deleted
(Added Delphi example) |
(added C sharp) |
||
Line 91: | Line 91: | ||
2 |
2 |
||
</pre> |
</pre> |
||
=={{header|C sharp}}== |
|||
<lang c sharp> |
|||
public static int CountSubStrings(string testString, string testSubstring) |
|||
( |
|||
int location = 0; |
|||
int count = 0; |
|||
while (location < testString.Length) |
|||
{ |
|||
int found = testString.IndexOf(testSubstring, location); |
|||
if (found == -1) |
|||
{ |
|||
break; |
|||
} |
|||
else |
|||
{ |
|||
count++; |
|||
location = location + testSubstring.Length; |
|||
} |
|||
} |
|||
return count; |
|||
}</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.algorithm; |
<lang d>import std.stdio, std.algorithm; |