当前位置:编程学习 > C#/ASP.NET >>

正则替换^

--------------------编程问答-------------------- 用Replace替换应该就可以吧

 string mm = " m^3t_4 ";
 string nn=mm.Replace("^", "<sup></sup>").Replace("_", "<sub></sub>");
--------------------编程问答--------------------
string str = "m^3t_4";
                str = Regex.Replace(str, @"[_\^](\d+)", a =>
                {
                    if (a.Value.Contains("^"))
                        return string.Format("<sup>{0}</sup>",a.Groups[1].Value);
                    else
                        return string.Format("<sub>{0}</sub>", a.Groups[1].Value);
                });
                //m<sup>3</sup>t<sub>4</sub>
--------------------编程问答--------------------
引用 1 楼 moyanruo 的回复:
用Replace替换应该就可以吧

 string mm = " m^3t_4 ";
 string nn=mm.Replace("^", "<sup></sup>").Replace("_", "<sub></sub>");


一看你程序就做的不好 初级程序员啊 --------------------编程问答--------------------
引用 3 楼 zhoufeng0401 的回复:
Quote: 引用 1 楼 moyanruo 的回复:

用Replace替换应该就可以吧

 string mm = " m^3t_4 ";
 string nn=mm.Replace("^", "<sup></sup>").Replace("_", "<sub></sub>");


一看你程序就做的不好 初级程序员啊

--------------------编程问答--------------------
引用 2 楼 Return_false 的回复:
string str = "m^3t_4";
                str = Regex.Replace(str, @"[_\^](\d+)", a =>
                {
                    if (a.Value.Contains("^"))
                        return string.Format("<sup>{0}</sup>",a.Groups[1].Value);
                    else
                        return string.Format("<sub>{0}</sub>", a.Groups[1].Value);
                });
                //m<sup>3</sup>t<sub>4</sub>


你这个通用吗 ,就是无论什么串 里边有^和_都能正确替换是吧?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,