Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary - StringComparison.CurrentCultureIgnoreCase

Tags:

c#

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");
            }
        }
    }
}
like image 308
Ivan Avatar asked Nov 12 '15 08:11

Ivan


1 Answers

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.

like image 147
Patrick Hofman Avatar answered Oct 16 '22 17:10

Patrick Hofman