我设计的应用程序,将在初始设置带来的应用程序使用。的设置是通过一个文本文件导入并都是具有以下格式:
Color= Blue
Size= 5
Value = 100
每一行是一个创建的对象设置(字符串,字符串b)使用的“=”与Line.Split分隔符()。然后将每个创建的对象添加到List(设置)_settingsList。
问题
有在文本文件中的最后2个设置,在格式
Location = 123 This Street, City, State, Zip Code
在这种情况下,我想通过拆分它既有“=”和“,”并用它来创建一个对象位置(字符串名称,字符串地址,字符串城市,字符串状态,字符串zip)。最后,这将被添加到List _locList。
当前代码
StreamReader reader = new StreamReader(openFileDialog1.FileName);
string line;
while ((line = reader.ReadLine()) != null)
{
string[] words = line.Split('=');
if(words[0].ToLower().Trim() == "Location")
{
string keepThis = words[0].ToLower().Trim();
string[] Words = line.Split('='); //how to split by 2 delimiters?
_locList.Add(new Location(Words[0], Words[1], Words[2], words[3], Words[4]);
}
_settingsList.Add(new Setting(words[0], words[1]));
}
字符串KEEPTHIS是存在的,因为我试图从线,使用删除整个“位置=”一部分“”作为分隔符,则只需添加它作为新的位置(保留这个,单词[0],单词[1],单词[2],单词[3])
任何建议,以帮助解决这个将非常感激!
'Split()'可以在参数中调用字符串数组:'mystring.Split(new string [] {“=”,“,”};' – KamikyIT