split("+")和split("")是不同的 [英] split(" +") and split(" ") are different

查看:64
本文介绍了split("+")和split("")是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想消除字符串中的真空.

I want to erase the vacuum in the String.

String input = "java example.java       aaa     bbb";
String[] temp = input.trim().split(" ");

结果是

java
example.java




aaa




bbb

java
example.java




aaa




bbb

但是我想要得到的结果

java
example.java
aaa
bbb

java
example.java
aaa
bbb

所以,我使用split("+").结果是正确的.但我不明白,怎么做split("+").

so, i use the split(" +"). The result is right. but i don't understand, how doing the split(" +").

推荐答案

split()使用正则表达式作为这是争论.正则表达式中的"+"表示上一个元素中的一个或多个".因此,在"+"上分割将在一个或多个空格"上分割.

split() takes a regex as it's argument. "+" in regex means "one or more of the previous element". So splitting on " +" will split on "one or more spaces".

这篇关于split("+")和split("")是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆