c#如何去除字符串中的空格,回车,换行符,制表符

如题所述

代码如下:

string l_strResult =  str.Replace("\n", "").Replace(" ","").Replace("\t","").Replace("\r","");

去除空格:s = s.replace('\\s','');

去除回车:s = s.replace('\n','');

这样也可以把空格和回车去掉,其他也可以照这样做。

注:\n 回车(\u000a) 

\t 水平制表符(\u0009) 

\s 空格(\u0008) 

\r 换行(\u000d)*/

扩展资料

在JAVA中去除字符串中的空格,回车,换行符,制表符的代码如下:

public class TxtWithoutNTSRElement { 

 public static String getTxtWithoutNTSRElement(String str){  

String dest = "";    

if (str!=null) {   

 Pattern p = Pattern.compile("[\\s]|[\t]|[\r]|[\n]|[?]|[^\\p{ASCII}]");   

 Matcher m = p.matcher(str);   

dest = m.replaceAll("");    

}    

 return dest;    

  }  

 public static void main(String[] args) { 

// String test="                                                        168.7";

//String test="s            srrttee  s  see??  ?";   

 String test="2011-01-01 ";

System.out.println(TxtWithoutNTSRElement.getTxtWithoutNTSRElement(test));  

  }  

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-01
string l_strResult = 你的字符串.Replace("\n", "").Replace(" ","").Replace("\t","").Replace("\r","");本回答被提问者和网友采纳
相似回答