一个字符串在其中的数字和字母之间插入空格
作者: 周建东 日期: 2007-06-22 06:06
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]", " $&"));
}
一个正则即可