Java:当单词之间的空格数量可变时,用空格拆分字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19019560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:26:26  来源:igfitidea点击:

Java: Splitting a string by whitespace when there is a variable number of whitespaces between words?

javastringsplit

提问by

I have the following:

我有以下几点:

String string = "1-50  of 500+";
String[] stringArray = string.split(" ");

Printing out all the elements in this array gives me the following:

打印出这个数组中的所有元素给我以下内容:

Element 1: 1-50
Element 2: ?of?500+

How can I get it to split elements by the requirement that there is at least one whitespace between the words?

我怎样才能通过要求单词之间至少有一个空格来拆分元素?

In other words, I want my elements to be:

换句话说,我希望我的元素是:

Element 1: 1-50
Element 2: of
Element 3: 500+

采纳答案by Sri Harsha Chilakapati

Use \\s+to split on spaces even if they are more.

使用\\s+对空间分割,即使他们都多。

String string = "1-50  of 500+";
String[] stringArray = string.split("\s+");

for (String str : stringArray)
{
    System.out.println(str);
}

Full example: http://ideone.com/CFVr6N

完整示例:http: //ideone.com/CFVr6N

EDIT:

编辑:

If you also want to split on tabs, change the regex to \\s+|\\t+and it detects both spaces and tabs as well.

如果您还想在制表符上拆分,请将正则表达式更改为\\s+|\\t+,它还会检测空格和制表符。

回答by newuser

String[] stringArray = string.split(" +");

instead of

代替

String[] stringArray = string.split(" ");

回答by Siva

You can split using regex

您可以使用正则表达式拆分

string.split("\s+");

回答by Vimal Bera

Use this to spilt the String.

使用它来溢出字符串。

String[] stringArray = string.split("\s+");

回答by Vicky Thakor

Your Program is correct but you put two space after 1-50. So Its giving you that output. Remove one space and you are done. No need to change the code. But if there is a two space then you should use string.split("\\s+")

您的程序是正确的,但您在1-50. 所以它给你那个输出。删除一个空格,你就完成了。无需更改代码。但是如果有两个空格那么你应该使用string.split("\\s+")

回答by Sri Harsha Chilakapati

With guava Splitter:

番石榴Splitter

package com.stackoverflow.so19019560;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import java.util.List;

public class Foo {

    private static final Splitter SPLITTER = Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings();

    public static void main(final String[] args) {
        final String string = "1-50  of \t\n500+";
        final List<String> elements = Lists.newArrayList(SPLITTER.split(string));

        // output
        for (int i = 0; i < elements.size(); i++) {
            System.out.println(String.format("Element %d: %s", i + 1, elements.get(i)));
        }
    }
}

Output:

输出:

Element 1: 1-50
Element 2: of
Element 3: 500+

回答by user2804925

To fix the space problem, after splitting, you can use .trim()to remove the white space.

解决空间问题,拆分后,可以使用.trim()删除空白。

Something like this should work.

像这样的事情应该有效。

for(int x=0; x<stringArray.length(); x++){
    stringArray[x] = stringArray[x].trim();
}

回答by sirmagid

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    String string = "1-50  of 500+";
    String[] stringArray = string.split("\s+");
    for (String str : stringArray)
      {
        System.out.println(str);
      }
   }
}