代码如下:
StreamReader my_stream_reader = new StreamReader("results.txt");
string getligne = my_stream_reader.ReadLine();
while (getligne.Contains(" "))
{
getligne = getligne.Replace(" ", " ");
}
string[] sArray = getligne.Split(' ');
MessageBox.Show(sArray[0], "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);results.txt中是如下的文件:
18 18 18 18 18 18 18 18 18 18 18 18.143 18.146 18.2 18.1788 18.1808 18.179 18.179 18.2 18.1836 18.1854 18.2可是结果还是第一行字符串,根本没有分隔成功。请指教。。
StreamReader my_stream_reader = new StreamReader("results.txt");
string getligne = my_stream_reader.ReadLine();
while (getligne.Contains(" "))
{
getligne = getligne.Replace(" ", " ");
}
string[] sArray = getligne.Split(' ');
MessageBox.Show(sArray[0], "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);results.txt中是如下的文件:
18 18 18 18 18 18 18 18 18 18 18 18.143 18.146 18.2 18.1788 18.1808 18.179 18.179 18.2 18.1836 18.1854 18.2可是结果还是第一行字符串,根本没有分隔成功。请指教。。
{
getligne = getligne.Replace(" ", " ");
}这样试试?
这一句有用吗?空格替换成空格?
StreamReader my_stream_reader = new StreamReader(@"E:\test.txt");
string getligne = my_stream_reader.ReadToEnd().ToString();
if (getligne.Contains(" "))
{
getligne = getligne.Replace(" ", " ");
}
string[] sArray = getligne.Split(' '); foreach (string s in sArray)
Console.WriteLine(s);结果:
18
18
18
18
18
18
18
18
18
18
18
18.143
18.146
18.2
18.1788
18.1808
18.179
18.179
18.2
18.1836
18.1854
18.2
string getligne = my_stream_reader.ReadToEnd().ToString();
if (getligne.Contains(" "))
{
getligne = getligne.Replace(" ", " ");
}
string[] sArray = getligne.Split(new char[]{' ','\r','\n'},StringSplitOptions.RemoveEmptyEntries); foreach (string s in sArray)
Console.WriteLine(s);结果:
18
18
18
18
18
18
18
18
18
18
18
18.143
18.146
18.2
18.1788
18.1808
18.179
18.179
18.2
18.1836
18.1854
18.2
StreamReader my_stream_reader = new StreamReader("results.txt");
string getligne = my_stream_reader.ReadLine().ToString();
if (getligne.Contains(" "))
{
getligne = getligne.Replace(" ", " ");
}
string[] sArray = getligne.Split(new char[]{' ','\r','\n'},StringSplitOptions.RemoveEmptyEntries);
int num = sArray.Count();
MessageBox.Show(num.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);至少我想看看数组里有几个吧,结果是1。头疼阿,还是不行吧。
string getligne = my_stream_reader.ReadToEnd().ToString(); string[] sArray = getligne.Split(new char[]{' ','\r','\n'},StringSplitOptions.RemoveEmptyEntries); foreach (string s in sArray)
Console.WriteLine(s);
我在控制台程序输出是正确的
foreach(string s in lst)
{
string[] arr= s.Split(new string[]{" ","\r\n"},StringSplitOptions.RemoveEmptyEntries);
}