将 CSV 字符串解析为整数数组
我有一个文本框字段输入 123,145,125 我将此字段分隔成整数数组。如果一切都解析正确,则验证该字段为 true 或 false。
代码:
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();
}
I have a text box field inputs 123,145,125 I to separate this field into an array of integers. And validate this field true or false if everything is parsed right.
CODE:
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;
}
//new Code:
private bool chkID(out int[] val) //bool satus for checkID function
{
string[] split = srtID.Text.Split(new char[1] {','});
List numbers = new List();
int parsed;
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
给定的函数似乎做得太多了。这是回答您的标题所暗示的问题的一个:
编辑(根据您对问题的评论)
您已经定义了该函数需要执行的三件事。现在您只需为每个创建方法即可。以下是我对如何实施它们的猜测。
现在您只需执行新函数:
The given function seems to do too much. Here's one that answers the question implied by your title:
EDIT (based on your comment on the question)
You've defined the three things this function needs to do. Now you just need to create methods for each. Below are my guesses for how you could implement them.
Now you just execute your new functions:
我真的很想对@Austin Salonen 的答案发表评论,但它不适合。对于所提出的问题来说,这是一个很好的答案,但我想更广泛地扩展 csv/int 转换部分的讨论。
foreach
循环替换为普通的for
循环。您可能最终会得到更简单的 IL(阅读速度更快)。请参阅 (http://www.codeproject.com/KB/cs/foreach.aspx< /a>, http://msdn.microsoft.com/en-us/ Library/ms973839.aspx [使用 For 循环进行字符串迭代 - 版本 1])。提议的“安全”功能(如果您不想知道坏值,则具有重载)...
提议的“快速”功能...
无论如何,希望这对某人有帮助。
I really wanted to comment on @Austin Salonen's answer, but it didn't fit. It is a great answer for the question asked, but i wanted to expand the discussion a bit more generally on csv/int conversion part.
foreach
loop for a plainfor
loop. You'll likely end up with simpler IL (read faster). See (http://www.codeproject.com/KB/cs/foreach.aspx, http://msdn.microsoft.com/en-us/library/ms973839.aspx [Use For Loops for String Iteration—version 1]).TryParse
and only adds the "good" values, another that is not as safe, but faster.Proposed "safe" function (with overload in case you don't want to know the bad values)...
Proposed "fast" function ....
Anyway, hope this helps someone.
可能值得您花时间查看一下这个 FileHelper 以及 CSV 阅读器
希望它们能帮助您...
小心,
汤姆
It might be worth your while to check out this FileHelper and also CSV Reader
Hope they will help you...
Take care,
Tom
有一个很好的免费库用于解析 CSV 文件: FileHelpers
There is a good free library for parsing CSV files: FileHelpers
在 .NET 2.0 中你可以这样写
In .NET 2.0 you could write
这很简单,而且我认为效果很好。它只返回有效数字:
This is simple and I think works pretty well. It only return valid numbers: