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

基本的にcust1[AddressLength1]、テキスト ファイル内の配列内の値と値を比較する必要があります。

4

3 に答える 3

2

大文字と小文字をチェックせずに 2 つの文字列を比較する簡単な方法を次に示します。

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

truehere はパラメーターの値として渡されます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 に答える
2

String.Compare() は、等値チェックで大文字と小文字を区別するかどうかを指定できるオプションのパラメーターを受け取ります。

投稿されたコードに応じて編集

両方の Compare と Index は、オプションの列挙型 StringComparison を取ります。StringComparison.OrdinalIgnoreCase を選択すると、大文字と小文字は無視されます。

于 2013-03-18T16:14:43.740 に答える
1

ファイルから読み取るかどうかは問題ではありません。静的文字列の Comapare 関数を使用して比較する場合は、次のようになります。

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

最後のパラメーターとして true を渡します。

于 2013-03-18T16:15:26.953 に答える