解析字符串并进入变化的texbox [英] Parse string and take into varuous texboxes
问题描述
所以我有一些字符串女巫被,分裂,当我分裂时将值转换为各种字符串,如果字符串是hello,world,并且当我解析此字符串时我有字符串i和字符串j我希望有这样的i =你好j =世界这里是我如何解析
so i have some string witch is splited by "," i need too when i split take values into various strings like this if string is "hello,world" and i have veriables string i and string j when i parse this string i want to have like this i="hello" j= "World" here is how i parse
string contents = File.ReadAllText(@"D:\test.txt");
char[] separatingChars = { ',' };
string text = contents;
string[] words = text.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);
我试过的只是重写所有价值
我试过的:
foreach(字符串s)
{
ie = s;
ge = s;
ce = s;
ye = s;
}
what i have tried is just it is rewriting all values
What I have tried:
foreach (string s in words)
{
ie = s;
ge = s;
ce = s;
ye = s;
}
推荐答案
Garth是对的 - 一旦你拆分数据就可以分配值。
但是......你似乎没有理解什么是循环确实如此!
当你编写这样的代码时:
Garth is right - once you split the data you can just assign values.
But...you don't seem to have understood what a loop does!
When you write code like this:
foreach (string s in words)
{
ie = s;
ge = s;
ce = s;
ye = s;
}
然后 words
集合中的每个字符串都将分配给 s
反过来 - 因为你设置了四个变量中的每一个,即
, ge
, ce
和 ye
到相同的值,他们确实都会获得相同的值 - 并且在循环完成后,它们都将包含最后一个字符串在集合中。
就像你编写这段代码一样:
then every single string in the words
collection will be assigned to s
in turn - and since you set each of the four variables ie
, ge
, ce
, and ye
to the same value they they will indeed all get the same value - and after the loop completes, they will all contain the last string in the collection.
It's just as is you had written this code instead:
ie = "hello";
ge = "hello";
ce = "hello";
ye = "hello";
ie = "world";
ge = "world";
ce = "world";
ye = "world";
如果你想分开这些值,那么你需要删除循环 - 我会直接继续实际的收集:
If you want to separate the values, then you need to remove the loop - and I'd continue with the actual collection directly:
string contents = File.ReadAllText(@"D:\test.txt");
string[] words = contents.Split(',');
if (words.Length != 4)
{
// Report problem to user
return;
}
ie = words[0];
ge = words[1];
ce = words[2];
ye = words[3];
这篇关于解析字符串并进入变化的texbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!