简体   繁体   中英

Why Contains just looks for one character instead of the word in the string?

Don't know why I can't search for complete words in the string ausgabe . Our Teacher gave us a .txt file with a text in ascii. Our task is to read in the file; find the right key and display the right text.

for (int i = 0; i < 256; i++)
{
    textBox3.Text = i.ToString(); 
    textBox3.Refresh();

    string text = textBox1.Text;
    byte[] uni = Encoding.Unicode.GetBytes(text);

    foreach (byte a in uni)
    {
        Convert.ToInt32(a);
        int zahl = a ^ i;
        string ende = Convert.ToString((char)zahl);
        textBox2.AppendText(ende);
    }

    string ausgabe = textBox2.Text;
    string suche = "die"; //just works if i'm looking for a single letter

    if (ausgabe.Contains(suche)) 
        textBox4.Text = ausgabe;
}

I'm in a vocational education to become a IT specialist and fairly new to programming (my fourth week of programming. Sorry for my bad english, it's not my native language.

I don't know why this doesn't work but did you try something like:

string ausgabe = textBox2.Text;
string suche = "die";
string suche2 = "der";

if(ausgabe.IndexOf(suche) != -1) textBox4.Text = ausgabe;

Maybe the IndexOf(string) method could help you.

EDIT: If it should find "Die" too, you have to translate the whole text first into lower cases. This can be simply achieved with

ausgabe = ausgabe.ToLower();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM