分解一串 [英] Breaking up a string
问题描述
大家好,
我正在从C#应用程序读取txt文件.读取
时
第一行(然后是下一行,等等).它被裁剪为"line"字符串.(line
按行)
在我的txt文件中,在该行中,我使用"*"分隔了值.
因此,我需要将其分解为单独的值.当它
看到*,它必须知道它是该值的结尾.
因此,逐行安装,我希望它逐个值.
请任何帮助将被应用!
这是我使用的代码:
Hi all,
I am reading a txt file from C# app. When it reads the
first line (and then the next etc..) it gets trown into the "line" string.(line
by line)
In my txt file, in the line I seperated the values using the "*"
So I need it to break up the line into seperate values. When it
sees a * it must know it is the end of that value.
So insted of line by line, I want it value by value.
Please any help would be appriciated!!
Here is the code I used:
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("C:/Users/user/Downloads/test1.txt");
while ((line = file.ReadLine()) != null)
{
MessageBox.Show(line);
counter++;
}
file.Close();
推荐答案
使用 ^ ]方法.
问候,
Manfred
Use the String.Split[^] method.
Regards,
Manfred
我创建了一个名为1.txt的文件,其值
I have created a file named 1.txt having value
asd*fasfda*fdaaaaa*aaaaaaaaa
dddddddd*ddd
现在,为了基于"*"分隔值,我使用了以下代码
Now for separating values based on "*" I have used following code
static void Main(string[] args)
{
string text="";
text = System.IO.File.ReadAllText(@"F:\Test\1.txt");
string[] Values = text.Split(''*'');
int counter = 0;
while (counter < Values.Length)
{
Console.Write(Values[counter++]);
Console.Write("\n");
}
}
请勿使用ReadLine()
以字符串值读取整个文件.然后用*
分开
Do not use ReadLine()
Read the whole file as a string value. Then separate it by *
System.IO.StreamReader file =
new System.IO.StreamReader("C:/Users/user/Downloads/test1.txt");
string wholeFile = file.ReadToEnd();
string[] separatedFile = wholeFile.Split('*');
这篇关于分解一串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!