使用Asp.Net计算字符串中的特定字符 [英] counting specific character in a string using Asp.Net

查看:96
本文介绍了使用Asp.Net计算字符串中的特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特定单词中,计算一个特定字符.
例如:在下载中计算"O"字.
结果计数:2

In Particular word, count a particular Character.
For Example : Count ''O'' word in Download.
Result Count : 2

推荐答案

:
myString.Count(ch => ch == 'C');


取决于您的喜好:

Depends on what you like:

// given the following, and assuming you don't care about case-sensitivity:
string myString = "Download";
char myChar = 'o';
int count = 0;

// if case-sensitivity matters, comment these two lines
myChar = myChar.ToLower();
myString = myString.ToLower()

// do this:
foreach (char c in myString)
{
    count += (c = myChar) ? 1 : 0;
}

// or this:
count = myString.Count(ch => ch == myChar);

// or this:
int count = (from c in myString
             where c == myChar
             select c).Count();


Dim s As String = "How are you doing?"
Dim Count As Integer = (From c As Char In s.ToCharArray Where c.Equals("o"c) Select c).Count



是使用LinQ的一种方法



Is one method using LinQ


这篇关于使用Asp.Net计算字符串中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆