当前位置:网站首页>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
*/边栏推荐
猜你喜欢

Batch deletion with RPM -e --nodeps

Safety 7.18 operation

Simulation of synchronization performance of BOC modulation and demodulation based on MATLAB, output tracking curve and identification curve under different lead lag code distance

Force buckle monotone stack

安全作业7.22

Les raccourcis clavier liés à l'onglet ne peuvent pas être utilisés après la mise à jour du vscode

VSCode的感叹号+tab的快捷键不能用,以及A-SOUL-live2d插件出问题的解决方法

他山之石 | 阿里多模态知识图谱探索与实践

Matlab simulation of solving multi-objective optimal value based on PSO optimization

在一个有序数组中查找具体的某个数字(二分查找or折半查找)
随机推荐
Batch deletion with RPM -e --nodeps
Blazor快速实现扫雷(MineSweeper)
Six ways of uniapp route jump
double类型精度丢失问题以及解决方法
第一篇 项目基本情况介绍
安全作业7.22
494. Objectives and
Smart headline: smart clothing forum will be held on August 4, and the whole house smart sales will exceed 10billion in 2022
【OpenCV 例程200篇】225. 特征提取之傅里叶描述子
Subsequence --- edit distance
易基因|靶基因DNA甲基化测序(Target-BS)
第三篇 RBAC权限管理 数据库设计详解
After vscode is updated, the shortcut keys related to tab cannot be used
MySQL执行顺序
Idea five free plug-ins to improve efficiency
Dynamic planning - force buckle
[200 opencv routines] 225. Fourier descriptor for feature extraction
修改ssh命令行[[email protected]]#颜色
STL map operation
基于PSO优化的多目标最优值求解matlab仿真