str="要清楚的字符串";
str=str.Replace(" ","");//先清除空格。
str=str.Replace("\r\n\r\n","\r\n");//再清除空行,原理:空行是连续的
//回车。但碰到连续两个空行只能清除一个,所以需要再来一次。
str=str.Replace("\r\n\r\n","\r\n");
如果要写简单一点就是:
str=str.Replace(" ","").Replace("\r\n\r\n","\r\n")Replace("\r\n\r\n","\r\n");
你可以做一个多行的TextBox来试验一下~~
用正则表达式 "\s"对应空白空行。
string str = " efew ef";
str = str.Trim();
stringBuilder sb=new stringBuilder(" tes t co");
sb.replace(" ","");
用Trim()方法