string.split的问题 [英] problem with string.split

查看:68
本文介绍了string.split的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string [] lines = File.ReadAllLines(@" c:\ text\history.txt");


foreach(字符串s in lines)

{

ArrayList results = new ArrayList();


string delimit =" ;;" ;;

string [] currentline = s.Split(" ;;");


MessageBox.Show(s);


}


我试图使用文本文件和数组将一些基本的数据库功能合并到我的

程序中。我没有使用''数据库''

技术,因为我不理解它们,并花了好几天尝试

,所以回到基本的文本文件。 />

我已经将一个文本文件的内容读入一个名为lines的数组,

逐行。这样可以正常工作。


文件的结构使得每一行代表一条记录。

每行记录遵循这种类型的模式: -


日期;时间 ;名称 ;地址 ;其他一些信息;更多信息;

等...


我正在尝试编写一些会返回记录数组的代码

匹配给定名称。

我的想法是依次取出文件的每一行,并将其分成一个

''currentline''数组,然后我可以查询

currentline数组的第二个元素,检查它是否与搜索名称匹配。


如果确实如此,我可以将当​​前行添加到结果中数组。

如果我不能忽略它并检查下一行。


然而它没有用,那就是'我使用Split的方式有问题,它似乎是在寻找一个数组分隔符,而不是一个

单字符串。

我有什么想法可以让它上班吗?


谢谢,


加里。

string[] lines = File.ReadAllLines(@"c:\text\history.txt");

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string delimit = ";";
string[] currentline = s.Split(";");

MessageBox.Show(s);

}

I am trying to incorporate some basic database functionality into my
programme using a text file and arrays. I am not using ''database''
technologies because i dont understand them, and have spent days trying
to so am going back to a basic text file.

I''m have read the contents of a text file into an array called lines,
line by line. This works fine.

The file is structured so that each line represents one record.
Each line record follows this type of pattern : -

date ; time ; name ; address ; some other info ; some more info ;
etc...

I am trying to write some code that will return an array of records
that match a given name.
My idea was take each line of the file in turn, and split it into a
''currentline'' array, I could then query the 2nd element of the
currentline array and check to see if it matches the search name.

If it does then I can add the currentline to the results array.
If it doesn''t I can ignore it and examine the next line.

However it''s not working, there''s something wrong with the way i''m
using Split, it seems to be looking for an array delimiter and not a
single string.

Any ideas how I can get it to work please?

Thanks,

Gary.

推荐答案

好的。我已经设法对字符串进行排序并且具有以下内容。


foreach(字符串在行中)

{

ArrayList results = new ArrayList();


string [] currentline = s.Split(" ;;" .ToCharArray());

}


我如何查询当前行数组的第二个元素来检查

是否与给定值匹配,如果是,则为字符串S到

结果数组,如果它没有继续foreach循环?


谢谢你,


Gary。

OK. I''ve managed to sort the string split now and have the following.

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string[] currentline = s.Split(";".ToCharArray());
}

How do i query the second elemenet of the currentline array to check if
it matches a given value, and if it does, and the string S to the
results array, and if it doesn''t continue with the foreach loop?

Thankyou,

Gary.


好的。我已经设法对字符串进行排序并且具有以下内容。


foreach(字符串在行中)

{

ArrayList results = new ArrayList();


string [] currentline = s.Split(" ;;" .ToCharArray());

} $ / $

我如何查询当前行数组的第二个元素,以检查

是否与给定值匹配,如果匹配,则为字符串S到

结果数组,如果它没有继续foreach循环?


谢谢你,


Gary。

OK. I''ve managed to sort the string split now and have the following.

foreach (string s in lines)
{
ArrayList results = new ArrayList();

string[] currentline = s.Split(";".ToCharArray());
}

How do i query the second element of the currentline array to check if
it matches a given value, and if it does, and the string S to the
results array, and if it doesn''t continue with the foreach loop?

Thankyou,

Gary.


尝试


string [] splitStrings = new string [] {" ;;" ;};


// ...


s.Split(splitStrings,StringSplitOptions.None);

// ...

但我认为你应该更好地学习如何使用数据库。

Try

string[] splitStrings = new string[] {";"};

//...

s.Split(splitStrings, StringSplitOptions.None);

//...
But i think you should better learn how to use a database.


这篇关于string.split的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆