在Java中的多个空格上分割字符串 [英] Splitting string on multiple spaces in java
本文介绍了在Java中的多个空格上分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何按空格分割字符串
我在解析文本文件时需要帮助.文本文件包含类似
I need help while parsing a text file. The text file contains data like
This is different type of file.
Can not split it using ' '(white space)
我的问题是单词之间的空格不相似.有时只有一个空格,有时会给出多个空格.
My problem is spaces between words are not similar. Sometimes there is single space and sometimes multiple spaces are given.
我需要以这样一种方式分割字符串,使我只会得到单词,而不是空格.
I need to split the string in such a way that I will get only words, not spaces.
推荐答案
str.split("\\ s +")
可以工作.正则表达式末尾的 +
将多个空格视为单个空格.它返回一个字符串数组( String []
),没有任何"
结果.
str.split("\\s+")
would work. The +
at the end of the regular-expression, would treat multiple spaces the same as a single space. It returns an array of strings (String[]
) without any " "
results.
这篇关于在Java中的多个空格上分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文