日期:2014-05-18  浏览次数:20419 次

一个字符串,如何在其中的数字和字母之间插入空格
如 "123abc456def "变成   "123   abc   456   def "

------解决方案--------------------
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);
------解决方案--------------------
你这么慷慨哦!100分!
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] ", " $& "));
}

一个正则即可