Dictionary - StringComparison.CurrentCultureIgnoreCase

This code is a simple dictionary translator. Can I use StringComparison.CurrentCultureIgnoreCasethat when entering a textBox1word that is not taken into account for registration, 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");
            }
        }
    }
}
+4
source share
1 answer

Yes, you must specify a comparator in the dictionary constructor (and not StringComparison):

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

This will make all evaluations of the key in accordance with the rules of comparison: case insensitive.

+6
source

Source: https://habr.com/ru/post/1615533/


All Articles