10

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,503
  • 3
  • 38
  • 76
ojek
  • 9,680
  • 21
  • 71
  • 110
  • 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

8 Answers8

20

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
  • 69,226
  • 18
  • 123
  • 176
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
  • 32,393
  • 7
  • 68
  • 89
1

Another simple alternative:

if(strings.Split(new [] { "a" }, StringSplitOptions.None).Length == 2)
Felix Keil
  • 2,344
  • 1
  • 25
  • 27
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,848
  • 2
  • 23
  • 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
0

You can check the length (minus 1) of the array myString.Split('a') assuming that you're searching for the occurrences of 'a'

kolin
  • 2,326
  • 1
  • 28
  • 46
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 15 '22 at 06:31
0

You can also try StringUtils.countMatches

gce
  • 1,563
  • 15
  • 17