0

I Want to see how many time's a string occurrs in a string. For example I want to see how many times 2018 occurs in this paragraph:

zaeazeaze2018 azeazeazeazeaze2018azezaaze azeaze4azeaze2018

In this case it is occuring 3 times.

I tried the following code

But the problem is that it always returns 0

And I can't find the mistake here:

public static string count(string k)
{
    int i = 0;
    foreach(var  line in k)
    {
        if (line.ToString().Contains("Bestellung sehen"))
        {
            i++;
            i = +i;
        }

    }
    return i.ToString();
}
H H
  • 263,252
  • 30
  • 330
  • 514
xtool
  • 31
  • 7
  • 1
    `k` is a `char`. You can easily spot these mistakes when you debug. Please learn how to debug your programs. – bolov Mar 29 '19 at 22:18
  • No i declared K as a string – xtool Mar 29 '19 at 22:20
  • You can use `Regular Expressions` for this. Take a look at this example: https://dotnetfiddle.net/PGgbm8 – Rahul Sharma Mar 29 '19 at 22:31
  • 3
    When `k` is a string then `line` is a char, even weirder. – H H Mar 29 '19 at 22:34
  • And "Bestellung" is not "2018" . Match the question with the samples exactly. – H H Mar 29 '19 at 22:38
  • 2
    I think all the regex solutions miss the mark, an appropriate answer should probably explain whats going wrong, a solution with loops so the OP knows what a loop solution would looks like and the algorithm involved, and then a regex example. The OP should probably also explain, if this needs to be case sensitive, and if there will be more than one occurrence per line – TheGeneral Mar 29 '19 at 22:47
  • 1
    I meant `line` is a `char` – bolov Mar 29 '19 at 22:57
  • Possible duplicate of [Count the number of times a string appears within a string](https://stackoverflow.com/questions/3016522/count-the-number-of-times-a-string-appears-within-a-string) – miss nefrat Mar 29 '19 at 23:54

4 Answers4

1

use this :

string text = "Hello2018,world2018\r\nWe have five 2018 here\r\n2018is coming2018"
int Counter = Regex.Matches(text, "2018").Count;
Console.WriteLine(Counter.ToString()); //write : 5
miss nefrat
  • 93
  • 1
  • 1
  • 9
1

You can use Regular Expressions to handle such cases. Regular expressions give you good flexibility over your pattern matching in a string. In your case, I have prepared a sample code for you using Regular Expressions:

using System;
using System.Text.RegularExpressions;
                
public class Program
{
    public static void Main()
    {
        string str="zaeazeaze2018azeazeazeazeaze2018azezaazeazeaze4azeaze2018";
        string regexPattern = @"2018";
        int numberOfOccurence = Regex.Matches(str, regexPattern).Count;
        
        Console.WriteLine(numberOfOccurence);
        
    }
}

Working example: https://dotnetfiddle.net/PGgbm8

If you will notice the line string regexPattern = @"2018";, this sets the pattern to find all occurences of 2018 from your string. You can change this pattern according to what you require. A simple example would be that if I changed the pattern to string regexPattern = @"\d+";, it would give me 4 as output. This is because my pattern will match all occurences of numbers in the string.

Community
  • 1
  • 1
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
  • Thank you so much for your help sir ! – xtool Mar 29 '19 at 22:56
  • @xtool Glad to help. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older SO which still don't have answers. For more information, take a look at our stackoverflow.com/help/accepted-answer – Rahul Sharma Mar 19 '20 at 09:05
1

This can be accomplished using Regular Expressions with the following:

using System.Text.RegularExpressions;

public static int count(string fullString, string searchPattern)
{
    int i = Regex.Matches(fullString, searchPattern).Count;
    return i;
}

For example, the following returns 2 as an int, not string:

count("asdfasdfasfdfindmeasdfadfasdasdfasdffindmesadf","findme")

I find this is quick enough for most of my use cases.

Jack Casey
  • 1,628
  • 11
  • 18
  • 1
    There's no need to use `.Cast()` just to get LINQ's `Count()` method since `MatchCollection` already has a `Count` property by virtue of its `ICollection` implementation. `.Count()` has to iterate the whole collection through the casting iterator. If nothing else, this is unnecessarily verbose. But if you scaled it up, this would stop being "quick enough" sooner than the other answers that already use the `Count` property directly, although practically speaking, the regex matching probably would be a larger expense than the iteration. – madreflection Mar 30 '19 at 00:34
  • Good point! I may have been riding the Linq hype train a bit too hard, looked right past that one. Edited :) – Jack Casey Mar 30 '19 at 06:49
-1

str is your String from your count method str2 is your substring which is Bestellung sehen

int n = str2.length
int k = 0;
for(int i=0;i < str.length; i++){
    if(str.substring(i,i+n-1)){
        k++;
        if(i+n-1 >= str.length){
            break;
        }
    }
}

return k.toString()
Sean
  • 41
  • 1
  • 11
  • Thank you but i don't know why I get an error saying that 'char' dosen't contain indexOf i think Line is char here for some reason – xtool Mar 29 '19 at 22:29
  • @xtool I did an update. check it you! I hope you will understand the concept and can implement it – Sean Mar 29 '19 at 22:36