问题1:要从一个字符串中查找出符合条件的子字符串,在输出字符串中将查找到的子字符串的颜色变为红色,请各位高手给予指点,这个问题已经卡了很长时间了。谢谢。
代码如下:
  string str = "abcdabcd";
  string str1 = "abc";
  if (str.Contains(str1))
  {
  if (str.IndexOf(str1) > 0)
  {
  //不会写了,请给予指点,没有具体代码,给个思路也行  
  }
  }
  

解决方案 »

  1.   


       string str = "abcdabcd";
                string str1 = "abc";
                if (str.Contains(str1))
                {
                    if (str.IndexOf(str1) > 0)
                    {
                        str = str.Replace(str1, "<span style=\"color: #FF0000\">" + str1 + "<span>");
                        Response.Write(str);
                    }
                }
      

  2.   

    B/S的好解决,C/S比较难B/S的同意楼上的
      

  3.   

    忘记说了,我在做一个windows窗体的应用程序
      

  4.   

    winform的使用 richtextbox 控件
      

  5.   

    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string str=@"";
    if (richTextBox1.Find(str)>0)
    {
    int pos=richTextBox1.Find(str);
    richTextBox1.SelectionStart=pos;
    richTextBox1.SelectionLength=str.Length; 
    richTextBox1.SelectionFont=fnt;
    richTextBox1.SelectionColor=Color.Read;

    Replace(str, "<span style=\"color: #FF0000\">" + str + "<span>");
      

  6.   

    用richTextBox实现吧  
              if (this.richTextBox1.SelectedText.Length > 0)
                {
                    this.richTextBox1.SelectionColor = Color.Red;               }
      

  7.   

    晕!字符串怎么可能有颜色。有颜色的是你的显示字符串的解析环境,你懂得用什么环境来展现字符串,才可能知道如何编写解析程序使得你的字符串去跟环境命令规范配对成功。其它时间,如果你根本不知道目标环境是什么,而想先用一种通用的语法把展现形式抽象地记录下来,那么你就需要自己假设一种展现环境,例如使用html或者xaml或者你自定义的规范,然后等展现的时候再把它从一种规范转换为另外一种规范来展现。因此,这里关键的是你自己的规范文档能力,而别人不可能先知先觉地解答“子字符串颜色”问题。
      

  8.   

    当然是用richTextbox控件来显示,谢谢给位高手的指点,我会进一步验证的
      

  9.   


    感谢7楼给予的帮助,我尝试了一下,确实可以改变在richTextBox中查找到的子字符串的颜色了,但由于我没有把问题表述清楚,所以到现在我的问题依然没有解决,因为我的RichTextBox的内容有很多,并且还有换行,如下图所示:但上面代码中的RichTextBox的find方法只能查找一次,对于后面的就无能为力了,我也查看了RichTextBox类的其他方法的说明,都没有找到合适的,在此还恳请高手们给予进一步的指点,万分感谢!!
      

  10.   

    由于图片没有上传成功,所以下面是查询出来后要显示在RichTextBox控件中的内容:1.  及壮,试吏,
      When he grew up, he took the tests for officials,
    2.  廷中吏无所不狎侮。
      There was none of the officials in the great hall whom he did not dare to treat cavalierly.
    3.  沛中豪杰吏闻令有重客,皆往贺。
      When the eminent and distinguished persons and officials of P'ei heard that the magistrate had an important guest, they all went to congratulate him.
    4.  萧何为主吏,主进,
      Hsiao Ho was the superintendent of officials and took charge of the offerings.
    5.  高祖为亭长,素易诸吏,
      Although Kao-tsu was [only] Chief of a t'ing, he used to treat his fellow-officials contemptuously,
      

  11.   

    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string str=@"";
    int iIndex = richTextBox1.Find(str);
    While iIndex >0
    {richTextBox1.SelectionStart=pos;
    richTextBox1.SelectionLength=str.Length;  
    richTextBox1.SelectionFont=fnt;
    richTextBox1.SelectionColor=Color.Read;
    iIndex = richTextBox1.Find(str,iIndex,RichTextBoxFinds.WholeWord);加这个
    }
    if (richTextBox1.Find(str)>0)
    {
    int pos=richTextBox1.Find(str);
    richTextBox1.SelectionStart=pos;
    richTextBox1.SelectionLength=str.Length;  
    richTextBox1.SelectionFont=fnt;
    richTextBox1.SelectionColor=Color.Read;
    }  
    Replace(str, "<span style=\"color: #FF0000\">" + str + "<span>");
      

  12.   

     int iIndex = this.richTextBox1.Find(this.textBox1.Text);
                while (iIndex > 0)
                {
                    this.richTextBox1.SelectionStart = iIndex;
                    this.richTextBox1.SelectionLength = this.textBox1.Text.Length;
                    //this.richTextBox1.SelectedText = this.textBox1.Text;
                    this.richTextBox1.SelectionColor = Color.Red;
                    iIndex = this.richTextBox1.Find(this.textBox1.Text,iIndex + this.textBox1.Text.Length,RichTextBoxFinds.WholeWord);
                }测试过的
      

  13.   

    感谢以上仁兄的热情帮助,就在刚才按照各位的提示把这个问题解决了,方法是按照15楼的写的,但将最后一个语句改为:iIndex = this.richTextBox1.Find(str, iIndex + str.Length, RichTextBoxFinds.None);      才运行成功。