C#在字符串拆分后使用TryGetValue
本文关键字:TryGetValue 拆分 字符串 | 更新日期: 2023-09-27 18:28:00
好吧,我已经搜索了所有地方,我真的被这个卡住了。我正在尝试创建一个程序,该程序将使用streamreader加载一个CSV文件,其中包含用逗号分隔的文本单词,然后将它们添加到词典中。然后在表单上,如果用户在第一个文本框中键入逗号之前的文本并单击按钮,则逗号之后的文本将显示在另一个文本框。
我不会撒谎,我仍在努力学习c#的基础知识,所以请给出一个解释性的答案!
这是我刚才的代码,我不知道该从哪里开始,我想在逗号分割后使用TryGetValue将文本的第一部分分配为[0],逗号后的第二部分分配为[1]
//Dictionary Load Button
private void button1_Click_1(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) // Allows the user to choose the dictionary to load
{
Dictionary<string, int> d = new Dictionary<string, int>();
using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] splitword = line.Split(',');
}
}
}
}
我的输入数据示例如下:
黑白
猫、狗
黄色、蓝色
Dictionary的TryGetValue()
方法的问题是,只有当字典条目的值是一个引用类型,该引用类型被用作某种累加器或正在以某种方式进行转换时,它才真正有了自己的作用:
public Dictionary<string,List<Widget>> LoadWidgetDictionary( IEnumerable<Widget> widgets )
{
Dictionary<string,List<Widget>> instance = new Dictionary<string,List<Widget>>() ;
foreach( Widget item in widgets )
{
List<Widget> accumulator ;
bool found = instance.TryGetValue( item.Name , out accumulator ) ;
if ( !found )
{
accumulator = new List<Widget>() ;
instance.Add( item.Name , accumulator ) ;
}
accumulator.Add(item) ;
}
return ;
}
如果你不这样做,你最好只是检查一下字典中是否有密钥:
public Dictionary<string,Widget> LoadWidgets( IEnumerable<Widget> widgets )
{
Dictionary<string,Widget> instance = new Dictionary<string,Widget>() ;
foreach ( Widget item in widgets )
{
if ( instance.ContainsKey( item.Name ) )
{
DisplayDuplicateItemErrorMessage() ;
}
else
{
instance.Add( item.Name , item ) ;
}
}
return instance ;
}
修改以添加建议
你可以试试这样的东西:
Dictionary<string,string> LoadDictionaryFromFile( string fileName )
{
Dictionary<string,string> instance = new Dictionary<string,string>() ;
using ( TextReader tr = File.OpenText( fileName ) )
{
for ( string line = tr.ReadLine() ; line != null ; line = tr.ReadLine() )
{
string key ;
string value ;
parseLine( line , out key , out value ) ;
addToDictionary( instance , key , value );
}
}
return instance ;
}
void parseLine( string line , out string key , out string value )
{
if ( string.IsNullOrWhiteSpace(line) ) throw new InvalidDataException() ;
string[] words = line.Split( ',' ) ;
if ( words.Length != 2 ) throw new InvalidDataException() ;
key = words[0].Trim() ;
value = words[1].Trim() ;
if ( string.IsNullOrEmpty( key ) ) throw new InvalidDataException() ;
if ( string.IsNullOrEmpty( value ) ) throw new InvalidDataException() ;
return ;
}
private static void addToDictionary( Dictionary<string , string> instance , string key , string value )
{
string existingValue;
bool alreadyExists = instance.TryGetValue( key , out existingValue );
if ( alreadyExists )
{
// duplicate key condition: concatenate new value to the existing value,
// or display error message, or throw exception, whatever.
instance[key] = existingValue + '/' + value;
}
else
{
instance.Add( key , value );
}
return ;
}
我现在只想做下面这样简单的事情:
if(splitword.Length != 2)
//Do something (log it, throw an error, ignore it, etc
continue;
int numberVal;
if(!Int32.TryParse(splitword[1], out numberVal))
//Do something (log it, throw an error, ignore it, etc
continue;
d.Add(splitword[0], numberVal);
我不在编译之前,所以这可能需要清理,但应该很接近。