我有数以千计的文本文件,每个文件包含文字只有一行。
I have thousands of text files, and each file contains just a single line of text.
任何单个文件看起来是这样的:
Any single file looks like this:
somevalue1|somevalue2|somevalue3|somevalue4|somevalue5
的格式不会改变,并从上面,你可以看到每个值由|分隔。而总有一些正是5个值中的每个文件。
The format never changes, and from above, you can see how each value is separated by a "|". And there are always exactly 5 values in each file.
是否有人可以帮助我在正确的方向?我不知道我怎么能去提取每个值,到一个单独的字符串,如:
Can someone please help me in the right direction? I'm not sure how I could go about extracting each value, into a separate string, like:
string value1,value2,value3,value4,value5;
value1=somevalue1;
value2=somevalue2;
等等等等。
我希望这是有道理的。和AP preciate在所有的帮助
I hope this makes sense. And appreciate any help at all
感谢您
String.Split
会替你:
string line = "somevalue1|somevalue2|somevalue3|somevalue4|somevalue5";
string [] parts = line.Split(new[] { '|' });
然后你可以从该阵列读取单独的值:
Then you can read the separate values from that array:
string value1 = parts[0];
string value2 = parts[1];
// and so on
现在我可能漂移稍微偏离主题,但我认为不同的值具有不同的意义,使他们可以被视为一个表中的列?如果是这样的话,我可能是有意义的创建更发现形式公开这些值的一类。让我们pretent的5个值是的的名字的姓名的公司的最喜欢的颜色和宠物独角兽的名字的。然后,您可以创建下面的类:
Now I might drift slightly off topic, but I assume that the different values have different meaning, so that they can be regarded as columns in a table? If that is the case, I might make sense to create a class that exposes these values in a more discoverable form. Let's pretent that the five values are first name, last name, company, favorite color and name of pet unicorn. Then you could create the following class:
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public string FavoriteColor { get; set; }
public string NameOfPetUnicorn { get; set; }
}
......,你会然后把从该行的值到这样的人,像这样:
...and you would then put the values from the line into such a person like so:
private static Person GetPerson(string line)
{
string [] parts = line.Split('|'); // as per Dan Tao's suggestion
return new Person {
FirstName = parts[0],
LastName = parts[1],
Company = parts[2],
FavoriteColor = parts[3],
NameOfPetUnicorn = parts[4]
};
}