Remove newline spaces and other symbols in the string

package sunline.common.logic.Utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 去掉字符串中的空格,回车符
 * @author 
 *
 */
public class Test {
	/**
	 * 方法一、正则表达式去掉数据中的空格和回车符
	 * @param str 传进来的字符串
	 * @return 去掉空格和回车符
	 */
	public static String rmString1 (String str){
		// 正则表达式匹配空格和换行符
		Pattern par = Pattern.compile("\\s*|\t|\r|\n");
		Matcher mch = par.matcher(str);
		// 返回数据
		return mch.replaceAll("");
	}
	
	/**
	 * 方法一、replaceAll方法去掉数据中的空格和回车符
	 * @param str 传进来的字符串
	 * @return 去掉空格和回车符
	 */
	public static String rmString2(String str){
		String s1 = str.replace(" ", "");
		return s1;
	}
	
	public static void main(String[] args) {
		String str ="1 2 3 4";
		String s = Test.rmString1(str);
		String s1 = Test.rmString2(str);
		System.out.println("********" + s);
		System.out.println("********" + s1);
	}
	/*
	 * 注:\n 回车(\u000a) 
	 *    \t 水平制表符(\u0009) 
	 *	  \s 空格(\u0008) 
	 *	  \r 换行(\u000d)
	 */
}

Guess you like

Origin blog.csdn.net/u013804636/article/details/66972512