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;
while (location < testString.Length)
public static int CountSubStrings(string testString, string testSubstring)
{
(
int found = testString.IndexOf(testSubstring, location);
int location = 0;
if (found == -1)
int count = 0;
while (location < testString.Length)
{
{
int found = testString.IndexOf(testSubstring, location);
break;
}
if (found == -1)
else
{
{
break;
count++;
}
location = location + testSubstring.Length;
else
{
count++;
location = location + testSubstring.Length;
}
}
}
return count;
}
}
return count;
}</lang>
}</lang>