Home guava example - Splitter
Post
Cancel

guava example - Splitter

view source

string을 구분자를 통해 Iterable 객체로 변경

1
2
3
4
5
6
7
    @Test
    public void splitTest() throws Exception {
        String sequence = "java ,scala, ,python       ,      go, , javascript";
        Iterable<String> iterable = Splitter.on(',')
                .split(sequence);
        System.out.println(iterable); // [java , scala,  , python       ,       go,  ,  javascript]
    }

list로 만들수도 있다.

1
2
3
4
5
6
7
    @Test
    public void splitToListTest() throws Exception {
        String sequence = "java ,scala, ,python       ,      go, , javascript";
        List<String> strings = Splitter.on(',')
                .splitToList(sequence);
        System.out.println(strings); // [java , scala,  , python       ,       go,  ,  javascript]
    }

공백문자 제거

1
2
3
4
5
6
7
8
    @Test
    public void splitTrimResultTest() throws Exception {
        String sequence = "java ,scala, ,python       ,      go, , javascript";
        Iterable<String> iterable = Splitter.on(',')
                .trimResults()
                .split(sequence);
        System.out.println(iterable); // [java, scala, , python, go, , javascript]
    }

null 오브젝트 제거

1
2
3
4
5
6
7
8
9
    @Test
    public void splitOmitEmptyTest() throws Exception {
        String sequence = "java ,scala, ,python       ,      go, , javascript";
        Iterable<String> iterable = Splitter.on(',')
                .trimResults()
                .omitEmptyStrings()
                .split(sequence);
        System.out.println(iterable); // [java, scala, python, go, javascript]
    }
This post is licensed under CC BY 4.0 by the author.