StreamReader区分大小写

时间:2013-03-18 16:12:22

标签: c# streamreader case-sensitive

我的程序当前正在读取一个文本文件,并将其与文本框中的值进行比较,然后告诉我有多少匹配,这当前有效。

我的疑问是它区分大小写。是否有任何方法可以使它无论是大写还是小写都无关紧要?

这是我的代码:

if (!String.IsNullOrEmpty(CustodianEAddress.Text))
{
    for (AddressLength1 = 0; AddressLength1 < Length; AddressLength1++)
    {
        List<string> list1 = new List<string>();
        using (StreamReader reader = new StreamReader(FileLocation))
        {
            string line1;
            //max 500
            string[] LineArray1 = new string[500];

            while ((line1 = reader.ReadLine()) != null)
            {
                list1.Add(line1); // Add to list.

                if (line1.IndexOf(cust1[AddressLength1].ToString()) != -1)
                {
                    count1++;
                    LineArray1[count1] = line1;
                }
            }

            reader.Close();
            using (System.IO.StreamWriter filed = 
               new System.IO.StreamWriter(FileLocation, true))
            {
                filed.WriteLine("");
                filed.WriteLine("The email address " + 
             cust1[AddressLength1].ToString() + " was found " + count1 +
             " times within the recipient's inbox");
            }

            string count1a;
            count1a = count1.ToString();
        }
    }
}
else
{
    MessageBox.Show("Please Enter an Email Address");
}

基本上,我需要将cust1[AddressLength1]中的值与文本文件中数组中找到的任何值进行比较。

3 个答案:

答案 0 :(得分:2)

String.Compare()接受一个可选参数,让您指定相等性检查是否区分大小写。

针对发布的代码进行了编辑

比较和索引两者都采用可选的枚举StringComparison。如果选择StringComparison.OrdinalIgnoreCase,则将忽略大小写。

答案 1 :(得分:2)

这是一种比较两个字符串而不检查大小写的快速方法:

string a;
string b;
string.Compare(a, b, true);

true此处作为ignoreCase参数的值传递,这意味着大写和小写字母将被比较,就好像它们都是相同的情况一样。

修改

我已经清理了一些代码,并且还放入了比较功能。我在更改内容时添加了评论:

// Not needed: see below.  List<string> list1 = new List<string>();

using (StreamReader reader = new StreamReader(FileLocation))
{
    string line1;
    //max 500
    List<string> LineArray1 = new List<string>();

    while ((line1 = reader.ReadLine()) != null)
    {
        // list1.Add(line1); // Add to list.

        // By adding to the list, then searching it, you are searching the whole list for every single new line - you're searching through the same elements multiple times.
        if (string.Compare(line1, cust1[AddressLength1].ToString(), true) == 0)
        {
            // You can just use LineArray1.Count for this instead. count1++;
            LineArray1.Add(line1);
        }
    }

    // Not needed: using() takes care of this.  reader.Close();
    using (System.IO.StreamWriter filed =
        new System.IO.StreamWriter(FileLocation, true))
    {
        filed.WriteLine(); // You don't need an empty string for a newline.
        filed.WriteLine("The email address " +
        cust1[AddressLength1].ToString() + " was found " + LineArray1.Count +
        " times within the recipient's inbox");
    }

    string count1a;
    count1a = LineArray1.Count.ToString();
}

答案 2 :(得分:1)

比较时,您正在从文件中读取这一事实并不重要 使用静态字符串Comapare函数:

public static int Compare(
    string strA,
    string strB,
    bool ignoreCase
)

并将true作为最后一个参数传递。