How to Count Strings in WPF

Asked 2 years ago, Updated 2 years ago, 116 views

In an event (Btn_Click), how do I count the target words, characters, etc. in a character?
I often see the following methods, but is it possible to work with buttons because it is static int?

static int countOccurrence (string str, string word) 
{ 
    // split the string by spaces 
    string [ ] a = str.Split('); 

    // search for pattern instring 
    int count = 0; 

    for (inti=0; i<a.Length;i++) 
    { 
        // if match found increase count 
        if(word.Equals(a[i])) 
            count++; 
    } 

    return count; 
}

c# wpf

2022-09-30 19:53

1 Answers

As for the static you are concerned about, it means that this method can be used independently of the instance, and there is no problem with Button_Click.
However, since this code has words separated by half-width spaces and counts the matching words among them, I don't think it can be used to find characters and strings in Japanese sentences.


2022-09-30 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.