一个字符串在其中的数字和字母之间插入空格

一个字符串在其中的数字和字母之间插入空格

字体大小:

         string str = "123abc456def";
         str = System.Text.RegularExpressions.Regex.Replace(str, @"(\d)([a-zA-Z])", "$1 $2");
         str = System.Text.RegularExpressions.Regex.Replace(str, @"([a-zA-Z])(\d)", "$1 $2");
     
         Response.Write(str);




string res = Regex.Replace("123abCc456def", @"(\d+)|(\s+)", " $1 $2", RegexOptions.Compiled | RegexOptions.IgnoreCase);
this.Label1.Text = res;




string str = "123abc456def";
System.Text.RegularExpressions.Regex.Replace(str, @"\d+", " $& ").Trim();
这样应该更简单吧,找出数字,然后再两加上空格就行了....最后Trim一下,把两头的空格去掉就行了..



using System.Text.RegularExpressions;

string[] ss = new string[] { "111aaa222ccc", "aaa111", "aaa" };
foreach (string s in ss)
{
    Console.WriteLine(Regex.Replace(s, @"(?<!\d|^)\d|(?<![a-z]|^)[a-z]", " $&"));
}

一个正则即可

引用通告地址: http://tmsoft.lsxy.com/trackback.php?tbID=256&extra=2834b0
原文地址:https://www.cnblogs.com/carl2380/p/1915704.html