1

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

我的问题是它区分大小写。有没有办法让它不管是大写还是小写?

这是我下面的代码:

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");
}

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

4

3 回答 3

2

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

为响应发布的代码而编辑

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

于 2013-03-18T16:14:43.740 回答
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();
}
于 2013-03-18T16:15:21.200 回答
1

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

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

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

于 2013-03-18T16:15:26.953 回答