java笔记_从String中移除空白字符

String对象为final类,所以字符串不能修改,使用下列方法后会得到一个新的字符串

trim()删除字符串开头和结尾的空格
strip()删除字符串开头和结尾的空格 (java11)
stripLeading()只删除字符串开头的空格 (java11)
stripTrailing()只删除字符串的结尾的空格 (java11)
replace()用新字符替换所有目标字符
replaceAll()将所有匹配的字符替换为新字符。此方法将正则表达式作为输入,以标识需要替换的目标子字符串
replaceFirst()仅将目标子字符串的第一次出现的字符替换为新的字符串

1.trim()

trim() : 删除字符串开头和结尾的空格

用法:

public static void main (String[] str) {
		String testString = " I am from China. ";
		System.out.println("testString : '" + testString + "'");
		System.out.println("testString : '" + testString.trim() + "'");
	}

输出:

testString : ' I am from China. '
testString : 'I am from China.'

但不是所有的 " " 都能删除,例:

public static void main (String[] str) {
		String testString = " I am from China. " + '\u2001';
		System.out.println("\\u2001 : '" + '\u2001' + "'");
		System.out.println("testString : '" + testString + "'");
		System.out.println("testString : '" + testString.trim() + "'");
	}
\u2001 : ' '
testString : ' I am from China.  '
testString : 'I am from China.  '

【原因】

trim移除的空白字符指的是ASCII值小于或等于32的任何字符('U+0020 ')


2.strip()

strip() : 删除字符串开头和结尾的空格,java11中新增方法

用法:

public static void main (String[] str) {
        String testString = " I am from China. " + '\u2001';
        System.out.println("\\u2001 : '" + '\u2001' + "'");
        System.out.println("testString : '" + testString + "'");
        System.out.println("testString : '" + testString.strip() + "'");
    }

输出:

\u2001 : ' '
testString : ' I am from China.  '
testString : 'I am from China.'

strip() 为Java11新增方法,根据Unicode标准判断字符是否为空白字符,比 trim() 更强大

Unicode是一个编码方案,Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。Unicode 编码共有三种具体实现,分别为utf-8,utf-16,utf-32,其中utf-8占用一到四个字节,utf-16占用二或四个字节,utf-32占用四个字节。Unicode 码在全球范围的信息交换领域均有广泛的应用。

【原理】

strip() 中使用 Character 类的 isWhitespace(int) 方法判断是否为空白字符,该方法使用unicode来标识空格字符。

System.out.println("'" + '\u2001' + "' 为空字符 : " +  Character.isWhitespace('\u2001'));

//  ' ' 为空字符 : true

3.stripLeading() 和 stripTrailing()

用法与 strip() 相同

testString.stripLeading();

testString.stripTrailing()

4.replace()

用指定的字符串替换每个目标子字符串。

public static void main (String[] str) {
        String testString = " I am from China. " + '\u2001';
        System.out.println("testString : '" + testString + "'");
        System.out.println("testString : '" + testString.replace(" ","") + "'");
    }
testString : ' I am from China.  '
testString : 'IamfromChina. '

特别需要注意的是,replace方法和trim方法一样,只能替换掉ASCII中的空白字符。

5.replaceAll()

最强大的字符串操作方法之一,使用正则表达式,就可以实现很多功能。

\s+       所有的空白字符

^\s+      字符串开头的所有空白字符

\s+$      字符串结尾的所有空白字符

注意,在java中要添加 \ 我们必须使用转义字符,所以对于 \s+ 我们必须使用 \\s+.

public static void main (String[] str) {
        String testString = " I am from China. ";
        System.out.println("testString : '" + testString + "'");
        System.out.println("replace all : '" + testString.replaceAll("\\s+","") + "'");
        System.out.println("replace start : '" + testString.replaceAll("^\\s+","") + "'");
        System.out.println("replace end : '" + testString.replaceAll("\\s+$","") + "'");
    }
testString : ' I am from China. '
replace all : 'IamfromChina.'
replace start : 'I am from China. '
replace end : ' I am from China.'

只能替换掉ASCII中的空白字符。

public static void main (String[] str) {
        String testString = " I am from China. " + '\u2001';
        System.out.println("testString : '" + testString + "'");
        System.out.println("replace all : '" + testString.replaceAll("\\s+","") + "'");
        System.out.println("replace start : '" + testString.replaceAll("^\\s+","") + "'");
        System.out.println("replace end : '" + testString.replaceAll("\\s+$","") + "'");
    }
testString : ' I am from China.  '
replace all : 'IamfromChina. '
replace start : 'I am from China.  '
replace end : ' I am from China.  '

【替换掉Unicode编码中的空白字符】

replaceAll("[\\pZ]", "")

\pP 其中的小写 p 是 property 的意思,表示 Unicode 属性,用于 Unicode 正表达式的前缀。

大写 P 表示 Unicode 字符集七个字符属性之一:标点字符。
其他六个是
L:字母;
M:标记符号(一般不会单独出现);
Z:分隔符(比如空格、换行等);
S:符号(比如数学符号、货币符号等);
N:数字(比如阿拉伯数字、罗马数字等);
C:其他字符

public static void main (String[] str) {
        String testString = " I am from" + '\u2001' + " China. ";
        System.out.println("testString : '" + testString + "'");
        System.out.println("replace all : '" + testString.replaceAll("[\\pZ]","") + "'");
    }
testString : ' I am from  China. '
replace all : 'IamfromChina.'

6.replaceFirst()

用法与 replaceAll() 相同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值