Best way to find how many times a string (or substring) occurs in a large C # string

For school, I had to complete an assignment that I had already passed, but the code I wrote is terrible, I don’t like what I ended up with. So, I'm curious what would be considered the best way to solve the following question in C #:

'// 4 How many times does the “queen” occur in Alice’s book in Wonderland? Write a code to count them.

link to book (pastebin): book

my code (pastebin): my code (ugly)

please write your answer, ignore my code. Also, explain what your code does and why you think this is the best solution. The number of times the word "queen" occurs in a book should be 76.

+4
source share
5 answers

I will not publish the full code, because, in my opinion, it is useful for you to try this as an exercise, but I personally would go for a decision from IndexOfwhich takes the initial position.

So something like (note: intentionally wrong):

int startingPosition = 0;
int numberOfOccurrences = 0;
do {
  startingPosition = fullText.IndexOf("queen", startingPosition);
  numberOfOccurrences++;
} while( matchFound );
+4
source

The shortest way to write. is the use of regex. he will find matches for you. just get the counts. Also the regex ignores the case variant, so you don't need to use ToLowerfor a large string. So after reading the file

string aliceFile = Path.Combine(Environment.CurrentDirectory, "bestanden\\alice_in_wonderland.txt");
string text = File.ReadAllText(aliceFile);

Regex r = new Regex("queen", RegexOptions.IgnoreCase);
var count = r.Matches(input).Count;

Also, since the input is very large, but the template is simple, you can use RegexOptions.Compiledit to make things faster.

Regex r = new Regex("queen", RegexOptions.IgnoreCase | RegexOptions.Compiled);
var count = r.Matches(input).Count;
+2
source

....

public static string[] Split(this string s, string separator)
{
    return s.Split(new string[] { separator }, StringSplitOptions.None);
}

.... And just use the string you are looking for as a specrator, and then the result is the length of the -1 array.

string s = "How now brown cow";
string searchS = "ow";
int count = s.split( seacrchS ).Length- 1;

The actual array returned by split will be ...

["H"," n"," b","n ","c"]

And ALWAAYS extension methods will come in handy again in the future.

+1
source

You can also use the regular expression:

 string s = "Hello my baby, Hello my honey, Hello my ragtime gal";
 int count = Regex.Matches(s, "Hello").Count;
+1
source

or you can use some linq to do the same thing

string words = "Hi, Hi, Hello, Hi, Hello";  //"hello1 hello2 hello546 helloasdf";
var countList = words.Split(new[] { " " }, StringSplitOptions.None);
int count = countList.Where(s => s.Contains("Hi")).Count();
0
source

Source: https://habr.com/ru/post/1616577/


All Articles