Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式java分割空间+,_Java_Regex_String_Split - Fatal编程技术网

正则表达式java分割空间+,

正则表达式java分割空间+,,java,regex,string,split,Java,Regex,String,Split,我有一个简单的问题。字符串如下所示: storm, whatever1, fire,water and something else,earth 我想使用String.split(“…”)和splitline表示“,”或“space+,”。只是不想在任何字符串中有任何空格。 结果应该是 String s[] = {"storm","whatever1","fire","water and something else","earth"}; 可以使用正则表达式吗?使用\\s*,\\s*拆分。这

我有一个简单的问题。字符串如下所示:

storm, whatever1, fire,water and something else,earth
我想使用String.split(“…”)和splitline表示“,”或“space+,”。只是不想在任何字符串中有任何空格。 结果应该是

String s[] = {"storm","whatever1","fire","water and something else","earth"};

可以使用正则表达式吗?

使用
\\s*,\\s*
拆分。这将在每个
上拆分,包括从每一侧围绕它的零个或多个空格。

使用
\\s*,\\s*
拆分。这将在每一个
上分割,包括从每一侧围绕它的零个或多个空格。

这就是您需要的:

public class Test
{
    public static void main(String[] args)
    {
        String source = "storm, whatever1, fire,water and something else,earth";
        for (String piece : source.split("\\s*,\\s*"))
        {
            System.out.println(piece);
        }
    }

}
这就是您需要的:

public class Test
{
    public static void main(String[] args)
    {
        String source = "storm, whatever1, fire,water and something else,earth";
        for (String piece : source.split("\\s*,\\s*"))
        {
            System.out.println(piece);
        }
    }

}

您使用的确切正则表达式是什么,可能存在问题。
split
使用正则表达式,如文档中所示。在
+
上拆分并删除空格很简单:
“..”。拆分(“\\s*,\\s*”)我可以在拆分后使用.trim(),但我不想做太多操作。您使用的确切正则表达式是什么,可能会有问题。
split
使用正则表达式,如文档中所示。在
+
上拆分并删除空格很简单:
“..”。拆分(“\\s*,\\s*”)我可以在分割后使用.trim(),但我不想做太多的操作..做。或者不要。没有尝试。或者不要。没有尝试。