当前位置:网站首页>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
*/边栏推荐
- What is the difference between server hosting and virtual host
- 智头条:智装论坛将于8月4日举行,2022全屋智能销售将破100亿
- Blazor快速实现扫雷(MineSweeper)
- Find the source code of the thesis
- 力扣-单调栈
- raid homes and plunder houses!
- C# 计算某个字符在字符串中出现的次数
- Smart headline: smart clothing forum will be held on August 4, and the whole house smart sales will exceed 10billion in 2022
- How to realize 485 wireless communication between multiple sensors and Siemens PLC?
- Full backpack!
猜你喜欢
随机推荐
信号量
Simulation of BOC modulation signal acquisition based on MATLAB
安全合理用电 收获清凉一“夏”
Leetcode: 17. letter combination of phone number
JSD-2204-会话管理-过滤器-Day19
Byte stream & character stream of IO stream
Supervisor installation and use
[CTFHub]JWT 的头部和有效载荷这两部分的数据是以明文形式传输的,如果其中包含了敏感信息的话,就会发生敏感信息泄露。试着找出FLAG。格式为 flag{}
Jsd-2204 session management filter day19
[turn] functional area division based on poi ()
Shell script case ---3
Camera 手电筒修改
BGP basic configuration
【7.16】代码源 -【数组划分】【拆拆】【选数2】【最大公约数】
go : gin Urlencoded 格式
Activity的启动流程
BGP federal experiment
深入理解CAS (自旋锁)
Unreal中通过FMonitoredProcess启动其他独立程序
基于matlab的BOC调制信号捕获仿真









