문제

I have a simple problem. String looks like this:

storm, whatever1, fire,water and something else,earth

I would like to use String.split(" ... ") and split line for "," or "space + ,". Just dont want to have any space in any of string. Result should be

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

Is it possible using regex?

도움이 되었습니까?

해결책

Split using \\s*,\\s*. This will split on each , including zero or more (*) whitespaces ("\\s") surrounding it from each side.

다른 팁

This is what you need:

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);
        }
    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top