gpt4 book ai didi

c# - 字典 - StringComparison.CurrentCultureIgnoreCase

转载 作者:太空狗 更新时间:2023-10-30 00:50:07 29 4
gpt4 key购买 nike

这段代码是一个简单的词汇-翻译器。我可以使用 StringComparison.CurrentCultureIgnoreCase,以便在您输入 textBox1 时不考虑注册单词吗?

using System.Text;
using System.IO;
using System.Windows.Forms;

namespace dс
{
public partial class Form1 : Form
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
StreamReader sr = new StreamReader("test.txt", Encoding.UTF8);

string[] split;
while (!sr.EndOfStream)
{
split = sr.ReadLine().Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries);
if (!dictionary.ContainsKey(split[0]))
{
dictionary.Add(split[0], split[1]);
}
}
sr.Close();
sr.Dispose();
}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
string word = textBox1.Text;
if (dictionary.ContainsKey(word))
{
textBox2.Text = dictionary[word];
}
else
{
MessageBox.Show("not found");
}
}
}
}

最佳答案

是的,您必须在字典的构造函数中指定比较器(不是 StringComparison):

new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase)

这将根据比较器的规则对键进行所有评估:不区分大小写。

关于c# - 字典 - StringComparison.CurrentCultureIgnoreCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33666929/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com