当前位置:网站首页>C # calculate the number of times a character appears in the string

C # calculate the number of times a character appears in the string

2022-07-23 15:23:00 User 4324355

C# Count the number of times a character appears in the string , It can be applied to calculate keyword density , Judge URL The level depth of the directory .

1. Use enumerable  Enumerable.Count()  Method , Reference space (System.Linq)

The recommended solution is to use System.Linq Of Count() Method to calculate the number of occurrences of a given character in a string . The method is as follows :

using System;
using System.Linq;
 
public class Example
{
    public static void Main()
    {
        string str = "Techie Delight";
        char ch = 'e';
 
        int freq = str.Count(f => (f == ch));
        Console.WriteLine(freq);
    }
}
 
/*
    Output: 3
*/

2. Use enumerable  Enumerable.Where()  Method , Reference space (System.Linq)

Here is another LINQ Solution , It USES Where() Method to filter strings . The following code example shows how to use this option :

using System;
using System.Linq;
 
public class Example
{
    public static void Main()
    {
        string str = "Techie Delight";
        char ch = 'e';
 
        int freq = str.Where(x => (x == ch)).Count();
        Console.WriteLine(freq);
    }
}
 
/*
    Output: 3
*/

3. Using string  String.Split()  Method

This is to use the specified characters to split the string into an array String.Split() Method , Through the string array Length Property to determine the count . An example of this method is shown below :

using System;
 
public class Example
{
    public static void Main()
    {
        string str = "Techie Delight";
        char ch = 'e';
 
        int freq = str.Split(ch).Length - 1;
        Console.WriteLine(freq);
    }
}
 
/*
    Output: 3
*/

4. Use  foreach  loop

We can also write our own logic for this simple task . The idea is to use foreach Loop iterates over the characters in the string , And keep the matching character count .

using System;
 
public class Example
{
    public static void Main()
    {
        string str = "Techie Delight";
        char ch = 'e';
 
        int freq = 0;
        foreach (char c in str)
        {
            if (c == ch) {
                freq++;
            }
        }
 
        Console.WriteLine(freq);
    }
}
 
/*
    Output: 3
*/

5. Use  Regex.Matches()  Method

Regular expressions Regex.Matches() Method is used to search the specified input string for all matches of the specified regular expression . We can use it to calculate the number of occurrences of characters in a string .

using System;
using System.Text.RegularExpressions;
 
public class Example
{
    public static void Main()
    {
        string str = "Techie Delight";
        char ch = 'e';
 
        int freq = Regex.Matches(str, ch.ToString()).Count;
        Console.WriteLine(freq);
    }
}
 
/*
    Output: 3
*/

Link to the original text

原网站

版权声明
本文为[User 4324355]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231018568907.html