8

I have this:

string strings = "a b c d d e";

And I need something similar to string.Contains(), but I need to know not only whether a string is present(in case above a letter), but also if it is present only ONE SINGLE time.

How can I achieve this?

Ken Kin
  • 4,351
  • 3
  • 37
  • 73
ojek
  • 8,947
  • 19
  • 68
  • 108
  • why not use a simple Linq statement to return the character that occurs the most you can use a `GroupBy` and `OrderDescending` – MethodMan Feb 08 '13 at 15:12
  • possible duplicate of [Find the most occurrence of a character in string C#?](http://stackoverflow.com/questions/5069687/find-the-most-occurrence-of-a-character-in-string-c) – MethodMan Feb 08 '13 at 15:13
  • I don't think that is a dupe. – Colonel Panic Feb 08 '13 at 15:20

6 Answers6

17

You can use LastIndexOf(String) and IndexOf(String) and verify that the values returned are equal. Of course also check if the String is found at all(i.e the returned value is not -1).

Ivaylo Strandjev
  • 66,530
  • 15
  • 117
  • 170
7

You could use LINQ

int count = strings.Count(f => f == 'd');
Andrew Beal
  • 427
  • 8
  • 23
1

An alternative

if(Regex.Matches(input,Regex.Escape(pattern)).Count==1)
Anirudha
  • 31,626
  • 7
  • 66
  • 85
1

Another simple alternative:

if(strings.Split(new [] { "a" }, StringSplitOptions.None).Length == 2)
Felix Keil
  • 2,184
  • 1
  • 23
  • 25
0

try like below... it 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());
Pandian
  • 8,428
  • 2
  • 19
  • 33
  • You seem to confuse your `strings` and `text` variables. For example when you say `text.IndexOf(text, i)`. Note that it might be interesting to know if the search string existed as two substrings which overlapped. Like if we searched for substring `"reassure"` inside `"XXXXXreassureassureXX"`, one could want to count two matches (not clear in the question). So watch how much you increment `i`. Also watch the increment of `i` because if you get out of the bounds of the `strings` variable, the next `IndexOf` will throw an exception, not just return `-1`. – Jeppe Stig Nielsen Feb 08 '13 at 16:12
  • First try your own code with `strings = "a b c d d e"` and `text = "e"`. After you fixed that problem, try you code with `strings = "XXXXXreassureassureXX"` and `text = "reassure"`. What answer do you prefer in that case? – Jeppe Stig Nielsen Feb 08 '13 at 16:38
0
    string source = "a b c d d e";
    string search = "d";
    int i = source.IndexOf(search, 0);
    int j = source.IndexOf(search, i + search.Length);
    if (i == -1)
        Console.WriteLine("No match");
    else if (j == -1)
        Console.WriteLine("One match");   
    else
        Console.WriteLine("More match");
Arvie
  • 47
  • 4