当前位置:网站首页>C# 计算某个字符在字符串中出现的次数
C# 计算某个字符在字符串中出现的次数
2022-07-23 10:20:00 【用户4324355】
C# 计算某个字符在字符串中出现的次数,可以应用于计算关键词密度,判断URL目录的层级深度。
1. 使用可枚举 Enumerable.Count() 方法,引用空间 (System.Linq)
推荐的解决方案是使用System.Linq的Count()方法来计算字符串中给定字符的出现次数。该方法如下所示:
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. 使用可枚举 Enumerable.Where() 方法,引用空间 (System.Linq)
下面是另一个LINQ解决方案,它使用Where()方法过滤字符串。下面的代码示例显示了如何使用此选项:
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. 使用字符串的 String.Split() 方法
这是使用指定的字符将字符串拆分为数组的String.Split()方法,通过字符串数组的Length属性来确定计数。这个方法示例如下所示:
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. 使用 foreach 循环
我们也可以为这个简单的任务编写自己的逻辑。其思想是使用foreach循环对字符串中的字符进行迭代,并保持匹配的字符计数。
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. 使用 Regex.Matches() 方法
正则表达式Regex.Matches()方法用于搜索指定正则表达式的所有匹配项的指定输入字符串。我们可以使用它来计算字符串中字符的出现次数。
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
*/边栏推荐
- How to realize 485 wireless communication between multiple sensors and Siemens PLC?
- [turn] functional area division based on poi ()
- MariaDB 数据库升级版本
- Regular expression common syntax parsing
- 494. Objectives and
- Simulation of voltage source PWM rectifier with double closed loop vector control based on Simulink
- Subsequence --- edit distance
- The accuracy of digital addition
- Byte stream & character stream of IO stream
- AVX指令集加速矩阵乘法
猜你喜欢

基于matlab的BOC调制解调的同步性能仿真,输出跟踪曲线以及不同超前滞后码距下的鉴别曲线

Common SQL of Oracle Report

Matlab simulation of depth information extraction and target ranging based on binocular camera images

Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot

Regular expression common syntax parsing

Redis | 非常重要的中间件

基于matlab的BOC调制信号捕获仿真

Version correspondence between numpy and pytorch

Simulink simulation of ESP three-phase SVPWM controller

Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing
随机推荐
Redis | 非常重要的中间件
Blazor快速实现扫雷(MineSweeper)
Simulink simulation of ESP three-phase SVPWM controller
The problem of double type precision loss and its solution
Simulation de modulation et de démodulation du signal CBOC basée sur MATLAB, sortie de corrélation, spectre de puissance et suivi de décalage de fréquence
报错 | cannot read property ‘_normalized‘ of undefined
易基因|靶基因DNA甲基化测序(Target-BS)
VSCode 更新后与tab相关快捷键无法使用
Supervisor installation and use
[turn] functional area division based on poi ()
day18
Live classroom system 03 model class and entity
The official redis visualization tool is coming. The needle doesn't poke
leetcode-设置交集大小至少为2
7.13WEB安全作业
Advanced operation and maintenance 03
@FeignClient使用詳細教程(圖解)
Matlab simulation of Turbo code error rate performance
【解决异常】Flink上传jar包至集群环境运行报未序列化异常
Force buckle monotone stack