This code is a simple vocabulary - translator. Can I use StringComparison.CurrentCultureIgnoreCase
, so that when you type in textBox1
not taken into account to register the word you?
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");
}
}
}
}
Yes, you have to specify the comparer in the constructor of the dictionary (not a StringComparison
):
new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase)
This will make all evaluations on the key act according to the rules of the comparer: case-insensitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With