java去除字符串中的空格、回车、换行符、制表符

  1. import java.util.regex.Matcher;  
  2. import java.util.regex.Pattern;  
  3.   
  4.   
  5.   
  6. /** 
  7.  * @author lei 
  8.  * 2011-9-2 
  9.  */  
  10. public class StringUtils {  
  11.   
  12.     public static String replaceBlank(String str) {  
  13.         String dest = "";  
  14.         if (str!=null) {  
  15.             Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
  16.             Matcher m = p.matcher(str);  
  17.             dest = m.replaceAll("");  
  18.         }  
  19.         return dest;  
  20.     }  
  21.     public static void main(String[] args) {  
  22.         System.out.println(StringUtils.replaceBlank("just do it!"));  
  23.     }  
  24.     /*----------------------------------- 
  25.  
  26.     笨方法:String s = "你要去除的字符串"; 
  27.  
  28.             1.去除空格:s = s.replace('\\s',''); 
  29.  
  30.             2.去除回车:s = s.replace('\n',''); 
  31.  
  32.     这样也可以把空格和回车去掉,其他也可以照这样做。 
  33.  
  34.     注:\n 回车(\u000a)  
  35.     \t 水平制表符(\u0009)  
  36.     \s 空格(\u0008)  
  37.     \r 换行(\u000d)*/  
  38. }  
import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
 * @author lei
 * 2011-9-2
 */
public class StringUtils {

	public static String replaceBlank(String str) {
		String dest = "";
		if (str!=null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}
	public static void main(String[] args) {
		System.out.println(StringUtils.replaceBlank("just do it!"));
	}
	/*-----------------------------------

	笨方法:String s = "你要去除的字符串";

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

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

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

	注:\n 回车(\u000a) 
	\t 水平制表符(\u0009) 
	\s 空格(\u0008) 
	\r 换行(\u000d)*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiTianao88

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值