如何计算逗号“,"的数量?在TextBox.text中? [英] How can I count the number of commas "," in a TextBox.text ?

查看:141
本文介绍了如何计算逗号“,"的数量?在TextBox.text中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

我使用VS.NET 2008在VB.NET中编写了一个小程序,对数字进行排序.

我从TextBox1.text中输入用逗号分隔的数字,播放某种算法,然后单击按钮,在TextBox2.text中显示排序后的值.

现在我想将数字限制为仅20个位置(例如32,1,6,32,12,21,21,54,675,8,4,3,2,9,0,21,21,21,43,744 )在TextBox1.text中,所以我想到了数逗号:laugh:

我可以这样做吗?还是有更好的方法来实现这一目标?


谢谢

Hi guyz,

I wrote a tiny programme in VB.NET using VS.NET 2008 to sort numbers.

I take the input of numbers separated by commas from TextBox1.text, play some algorithm then by clicking a button, display the sorted values in TextBox2.text.

NOW i want to restrict the numbers to be only 20 positions (eg. 32,1,6,32,12,21,21,54,675,8,4,3,2,9,0,21,21,21,43,744) in the TextBox1.text and so i thought of counting the commas :laugh:

Can I do that, how? OR is there any better way to achieve this?


Thanks

推荐答案

无需计算逗号.
使用Textbox1.Text.Split('','').Length.它为您提供了用逗号分隔的文本框中添加的数字的计数.
No need to count the commas.
Use Textbox1.Text.Split('','').Length. It gives you the count of the numbers added in the textbox separated by comma.


您可以采用以下规则的表达方式:
昏暗的有效AS布尔值= Regex.IsMatch(TextBox1.Text,"^(,?\ d +){0,20}
You could do it the reqular expression way:
Dim valid AS Boolean = Regex.IsMatch(TextBox1.Text, "^(,?\d+){0,20}


")

这也将验证输入,数字或逗号以外的任何内容都将返回false.
")

This would also validate the input, anything other than numbers or commas would return false.


这篇关于如何计算逗号“,"的数量?在TextBox.text中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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