How can I check if a string contains a single occurrence of a substring?
6 answers
try as below ... this will help you ...
string strings = "a b c d d e";
string text ="a";
int count = 0;
int i = 0;
while ((i = strings.IndexOf(text, i)) != -1)
{
i += text.Length;
count++;
}
if(count == 0)
Console.WriteLine("String not Present");
if(count == 1)
Console.WriteLine("String Occured Only one time");
if(Count>1)
Console.WriteLine("String Occurance : " + count.Tostring());
0
source to share