简体   繁体   English

C#字符串语法错误

[英]C# string syntax error

I'm reading in data from a file and trying to write only the word immediately before 'back' in red text. 我正在从文件中读取数据,并尝试仅在红色文本“后退”之前仅写单词。 For some reason it is displaying the word and then the word again backwords. 由于某种原因,它先显示单词,然后再显示单词。 Please help. 请帮忙。 Thank you. 谢谢。

private void Form1_Load(object sender, EventArgs e)
        {
            Regex r = new Regex(" ");
            StreamReader sr = new StreamReader("KeyLogger.txt");
            string[] tokens = r.Split(sr.ReadToEnd());
            int index = 0;
            for(int i = 0; i <= tokens.Length; i++)
            {
                if (tokens[i].Equals("back"))
                {
                    //richTextBox1.Text+="TRUE";
                    richTextBox1.SelectionColor = Color.Red;
                    string myText;
                    if (tokens[i - 1].Equals("back"))
                        myText = "";
                    else
                        myText = tokens[i - 1];
                    richTextBox1.SelectedText = myText;
                    richTextBox1.Text += myText;
                }
                else
                {
                    //richTextBox1.Text += "NOOOO";
                }

                //index++;
                //richTextBox1.Text += index;
            }
        }

The problem is with the line 问题是线

richTextBox1.SelectedText = myText; 

this is adding myText to the beginning of the text of the RichTextBox , if you remove this line you will get just the words preceeding back appended to the control in the order they appear. 这是将myText添加到RichTextBox的文本的开头,如果删除此行,您将只获得前面的单词,并按它们出现的顺序附加到控件上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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