Count occurrences of a substring: Difference between revisions
Content added Content deleted
(added C sharp) |
(→{{header|C sharp}}: added using and class) |
||
Line 94: | Line 94: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
<lang c sharp> |
<lang c sharp> |
||
using System; |
|||
public static int CountSubStrings(string testString, string testSubstring) |
|||
( |
|||
class SubStringTestClass |
|||
int location = 0; |
|||
{ |
|||
int count = 0; |
|||
public static int CountSubStrings(string testString, string testSubstring) |
|||
( |
|||
int |
int location = 0; |
||
int count = 0; |
|||
while (location < testString.Length) |
|||
{ |
{ |
||
int found = testString.IndexOf(testSubstring, location); |
|||
⚫ | |||
if (found == -1) |
|||
{ |
|||
break; |
|||
} |
|||
else |
|||
⚫ | |||
⚫ | |||
location = location + testSubstring.Length; |
|||
} |
|||
} |
} |
||
return count; |
|||
} |
} |
||
⚫ | |||
}</lang> |
}</lang> |
||