【发布时间】:2010-12-18 02:04:23
【问题描述】:
我有一个文本框字段输入 123,145,125 I 来将该字段分隔成一个整数数组。如果一切都正确解析,则验证此字段的真假。
代码:
private bool chkID(out int[] val)
{
char[] delimiters = new char[] { ',' };
string[] strSplit = iconeID.Text.Split(delimiters);
int[] intArr = null;
foreach (string s in strSplit) //splits the new parsed characters
{
int tmp;
tmp = 0;
if (Int32.TryParse(s, out tmp))
{
if (intArr == null)
{
intArr = new int[1];
}
else
{
Array.Resize(ref intArr, intArr.Length + 1);
}
intArr[intArr.Length - 1] = tmp;
}
if (Int32.TryParse(iconeID.Text, out tmp))
{
iconeID.BorderColor = Color.Empty;
iconeID.BorderWidth = Unit.Empty;
tmp = int.Parse(iconeID.Text);
val = new int[1];
val[0] = tmp;
return true;
}
}
val = null;
ID.BorderColor = Color.Red;
ID.BorderWidth = 2;
return false;
}
//新代码: private bool chkID(out int[] val) //checkID 函数的布尔值 { string[] split = srtID.Text.Split(new char[1] {','}); 列表编号 = new List(); int 已解析;
bool isOk = true;
foreach( string n in split){
if(Int32.TryParse( n , out parsed))
numbers.Add(parsed);
else
isOk = false;
}
if (isOk){
strID.BorderColor=Color.Empty;
strID.BorderWidth=Unit.Empty;
return true;
} else{
strID.BorderColor=Color.Red;
strID.BorderWidth=2;
return false;
}
return numbers.ToArray();
}
【问题讨论】:
-
告诉我们您当前的代码有什么问题是个好主意。
-
它是什么语言。我猜是 C#,但那是因为我知道它不是 Java。
-
我遇到的问题是我需要在文本字段中下载给定 ID 的 XML 数据。例如:“123,456,789”将此 CSV 字符串解析为整数数组,验证字段,然后下载有效 ID 的 XML 数据,谢谢 Chad