计算字符串中的字符数 [英] Count the number of characters in a string

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

问题描述

大家好


使用Visual Studio C#


有一个字符串


string st =这些椒盐脆饼让我口渴;喝这茶。

像heck一样跑。


******

我怎么去计算此字符串中的字符数

,不包括逗号,分号,空格和句点。


想在标签中显示数字结果。


如有任何建议,我们将不胜感激。


bebop


***通过Developersdex发送< a rel =nofollowhref =http://www.developersdex.comtarget =_ blank> http://www.developersdex.com ***

解决方案

2个字:正则表达式。 :)


类似于以下内容:


匹配m = Regex.Match(st,[az] [AZ] [ 0-9]");

if(m.Match)

{

Debug.WriteLine(m.Length);

}


(我没有测试代码,所以你可能必须使用正则表达式。)


HTH,

~d

cw bebop写道:

大家好

使用Visual Studio C#

有一个字符串

字符串st ="这些椒盐脆饼让我口渴;喝这茶。
跑得像哎。

******
我怎么去计算这个字符串中的字符数
包括逗号,分号,空格和句号。

想在标签中显示数字结果。

任何建议都将受到赞赏。

bebop

***通过开发人员指南 http://www.developersdex .com ***



cw bebop< cw **** @ yahoo.com>写在

新闻:eY ************** @ tk2msftngp13.phx.gbl:

大家好/>
使用Visual Studio C#

有一个字符串

字符串st ="这些椒盐脆饼让我口渴;喝这个茶。像heck一样跑。

******
如何计算此
字符串中的字符数而不包括逗号,分号,空格,和
期间。

想在标签上显示数字结果。




bebop,


正则表达式可用于捕获字符串中

字母的数量:

使用System.Text.RegularExpressions;


....


string st ="这些椒盐脆饼让我口渴;喝这茶。像heck一样运行。;

this.Label1.Text = Regex.Matches(st,[a-zA-Z]")。Count.ToString();

-

希望这会有所帮助。


克里斯。

---------- ---

CR Timmons Consulting,Inc。
http: //www.crtimmonsinc.com/


请通过正则表达式会员,您将得到所需的一切。一个

的替代建议是如果你有一套你想忽略的字符

在计数期间,请在正则表达式中使用替换动词。


Hi all

Using Visual Studio C#

Have a string

string st = "Hi, these pretzels are making me thirsty; drink this tea.
Run like heck."

******
How would I go about counting the number of characters in this string
without including the commas, semicolons, spaces, and periods.

Would like to display the number result in a label.

Any suggestions would be appreciated.

bebop

*** Sent via Developersdex http://www.developersdex.com ***

解决方案

2 words: Regular Expressions. :)

Something along the lines of:

Match m = Regex.Match(st, "[a-z][A-Z][0-9]");
if(m.Match)
{
Debug.WriteLine(m.Length);
}

(I didn''t test the code, so you may have to play with the regex expression.)

HTH,
~d

cw bebop wrote:

Hi all

Using Visual Studio C#

Have a string

string st = "Hi, these pretzels are making me thirsty; drink this tea.
Run like heck."

******
How would I go about counting the number of characters in this string
without including the commas, semicolons, spaces, and periods.

Would like to display the number result in a label.

Any suggestions would be appreciated.

bebop

*** Sent via Developersdex http://www.developersdex.com ***



cw bebop <cw****@yahoo.com> wrote in
news:eY**************@tk2msftngp13.phx.gbl:

Hi all

Using Visual Studio C#

Have a string

string st = "Hi, these pretzels are making me thirsty; drink
this tea. Run like heck."

******
How would I go about counting the number of characters in this
string without including the commas, semicolons, spaces, and
periods.

Would like to display the number result in a label.



bebop,

A regular expression can be used to capture the number of
letters in the string:
using System.Text.RegularExpressions;

....

string st = "Hi, these pretzels are making me thirsty; drink this tea. Run like heck.";
this.Label1.Text = Regex.Matches(st, "[a-zA-Z]").Count.ToString();
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/


Please go through Regex members, you will fine what you need. One
alternate suggestion is if you have set of charaters you want to ignore
during the count, use replace verb in the Regex.


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

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