使用正则表达式替换字符串

实现效果:

  

知识运用:

  Regex类的Replace()方法:用于替换在指定字符串内匹配正则式的字符串为某字符串

  public static string Replace(string input,string pattern,string replacement)

  input   要搜索匹配项的字符串

  pattern    要匹配的正则表达式模式

  replacement    要替换的为结果的字符串

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            string resoult = System.Text.RegularExpressions.
                Regex.Replace(textBox1.Text,"[A-Za-z]+",textBox2.Text);
            MessageBox.Show("原字符串:\n"+textBox1.Text+"\n"+
                    "替换为:\n"+textBox2.Text+"\n"+"替换后:\n"+resoult);
        }

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10045778.html
今日推荐