//自定义函数,参数一个是RichTextBox类型,一个是字符串类型
public void HighLightText(RichTextBox rtb,string str)
{
rtb.Text = str;
Stack stack = new Stack(); //创建一个栈
int i = 0;
int m = 0;
while (i < rtb.Text.Length)
{
m= rtb.Text.IndexOf("\"",i,rtb.Text.Length-i); //找到一个\""
if (m != -1)
{
i = m;
if (stack.Count == 0) //如果栈里面没有元素
{
stack.Push(m); //压进栈
else
{
rtb.SelectionStart = Convert.ToInt32(stack.Peek()); //把元素从栈中提出
rtb.SelectionLength = m - Convert.ToInt32(stack.Peek())+1; //得到要变色的字符长度
rtb.SelectionColor = Color.Red;
stack.Pop(); //删除栈头元素
}
i++;
}
}