检查字符串是否为整数 [英] checking string for integers
问题描述
好吧,这将是一个很奇怪的问题,不会撒谎.我有一台设备,当我按下按钮时,它会发送一个字符串,例如"135,125,154,458".我使用拆分和修剪"(Split and Trim)来获取所需的数字,并使用数字绘制一个矩形.但是,当我同时按下多个按钮时,它会发送奇怪的字符,因此会崩溃.我需要检查字符串,以确保它仅包含,"和数字",以便如果没有其他内容,则该字符串不会通过.有办法吗?
Ok this is going to be a weird question not gonna lie. I have a device and when i hit a button it sends a string like "135,125,154,458". I use Split and Trim and get the numbers which I need and draw a rectangle using the numbers. However when i hit multiple buttons at the same time it sends weird characters therefore it crashes. I need to check the string to make sure it contains only "," and "numbers" so that if there is anything other than those it won''t pass it. Is there a way to do it?
private void InputBox_TextChanged(object sender, EventArgs e)
{
if (InputBox.Text.StartsWith("π") == false)//Clear the box if the message does not start with π
{
InputBox.Text = "";
}
if ((InputBox.Text.StartsWith("π") == true) && (InputBox.Text.EndsWith("}") == true))// only process if the message starts with π and ends with }
{
string Message = InputBox.Text;
InputBox.Text = "";// Clear the box when done.
if (Message.StartsWith("πImageBox_Coordinates{") == true) ///Coordinates
{
Message = Message.Substring(22);
Message = Message.TrimEnd(''}'');
string[] MessageSplit = Message.Split(new char[] { '','' });
for (int i = 0; i < MessageSplit.Count(); i++)
{
buttcorrs[i] = Convert.ToInt16(MessageSplit[i]);
它崩溃在我转换它的最后一行.当我一一击中它时效果很好,但我也需要多次击中,因此,如果有人有想法将不胜感激.
谢谢,
it crasher at the last line where i convert it. It works great when i hit it one by one but I also need multiple hits so if anyone has an idea would appreciate it.
Thank you,
推荐答案
我不得不花很多时间才能使它工作,因为我对正则表达式并不熟悉,但是这里我认为最有效的方法.
使用System.Text.RegularExpressions库.
其中有一个名为RegEx
的类.
创建一个新的RegEx
类,如:
I had to play around with it quite a bit to get it to work as I am not that familiar with regular expressions, but here''s what I believe will work best.
Use the System.Text.RegularExpressions library.
Within it is a class calledRegEx
.
Create a newRegEx
class like:
Regex myRegEx = new RegEx("^\d+((,\d+)*)
")
(那是我从VB笨拙的转换)...在C#中不起作用.在C#中,应为:
(that was my clumsy conversion from VB)...that won''t work in C#. In C#, it should be:
RegEx myRegEx = new RegEx(@"^\d+((,\d+)*)
" );
");
然后,使用myRegEx.IsMatch(stringToTest)
传递要匹配的字符串.
如果为真,则应仅包含字母数字和逗号.如果为假,则还有其他内容.
我测试了以下内容,并得到了以下结果:
"483,3929,2020" True
"3939,2902s,1292"错误
"A394"错误
"394,394" True
"394,394s"错误
"394" True
因此,在您不熟悉正则表达式的情况下,我将尽力解释它的作用.
脱字号(^)告诉它从头开始. "\ d +"告诉它寻找一个或多个数字.因此,第一个块"^ d \ +"表示从头开始并检查尽可能多的数字.然后移至下一个块(((,\ d +)*)
Then, pass the string you want to match in using myRegEx.IsMatch(stringToTest)
.
If it is true, then it should only have letters numbers and commas. If false, it has something else.
I tested the following and got the following results:
"483,3929,2020" True
"3939,2902s,1292" False
"A394" False
"394,394" True
"394,394s" False
"394" True
So, I''ll try to explain what it''s doing in case you aren''t familiar with regular expressions.
The caret (^) tells it to start at the beginning. "\d+" tells it to look for one or more digits. So the first block "^d\+" says "start at the beginning and check for as many digits as there are. Then it moves on to the next block ("((,\d+)*)
这篇关于检查字符串是否为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!