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


实现效果:

  

知识运用:

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

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

  input   要搜索匹配项的字符串

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

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

实现代码:

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM