guava的splitter的使用,字符串转集合

public class SplitterTest {

    @Test
    public void testSplitOnSplit() {
        // Splitter.on("|").splitToList() 用"|"分割的字符串转为List集合
        List<String> resList = Splitter.on("|").splitToList("hello|world");
        Assert.assertNotNull(resList);
        Assert.assertEquals(resList.get(0), "hello");
        Assert.assertEquals(resList.get(1), "world");
    }

    @Test
    public void testSplitOnSplitOmitEmpty() {
        // Splitter.on("|").splitToList() 用"|"分割的字符串转为List集合,忽略空值
        List<String> resList = Splitter.on("|").splitToList("hello|world|||");
        Assert.assertNotNull(resList);
        Assert.assertEquals(resList.size(), 5);
    }

    @Test
    public void testSplitOnSplitOmitEmptyTrimResult() {
        // Splitter.on("|").splitToList() 用"|"分割的字符串转为List集合,忽略空值,并去除空格
        List<String> resList = Splitter.on("|").splitToList("hello | world| ||");
        Assert.assertNotNull(resList);
        Assert.assertEquals(resList.size(), 5);
    }

    @Test
    public void testSplitFixLength() {
        // Splitter.fixedLength(4).splitToList() 分割的固定长度为4的字符串转为List集合
        List<String> resList = Splitter.fixedLength(4).splitToList("aaaabbbbcccc");
        Assert.assertNotNull(resList);
        Assert.assertEquals(resList.size(), 3);
        Assert.assertEquals(resList.get(0), "aaaa");
        Assert.assertEquals(resList.get(1), "bbbb");
    }

    @Test
    public void testSplitOnSplitLimit() {
        // Splitter.on("#").limit(3).splitToList() 用#将分割字符串成3份,转为List集合
        List<String> resList = Splitter.on("#").limit(3).splitToList("aaaa#bbbb#cccc#dddd");
        Assert.assertNotNull(resList);
        Assert.assertEquals(resList.size(), 3);
        Assert.assertEquals(resList.get(0), "aaaa");
        Assert.assertEquals(resList.get(1), "bbbb");
        Assert.assertEquals(resList.get(2), "cccc#dddd");
    }

    @Test
    public void testSplitOnPatternString() {
        // splitter.onPattern("\\#").trimResults().omitEmptyStrings().splitToList()
        // 用#将分割字符串,转为List集合
        List<String> resList = Splitter.onPattern("\\#")  // 等同于Splitter.on(Pattern.compile("\\#"))
                .trimResults()
                .omitEmptyStrings()
                .splitToList("aaaa  #bbbb  ##cccc#dddd  ");
        Assert.assertNotNull(resList);
        Assert.assertEquals(resList.size(), 4);
        Assert.assertEquals(resList.get(0), "aaaa");
        Assert.assertEquals(resList.get(1), "bbbb");
        Assert.assertEquals(resList.get(2), "cccc");
        Assert.assertEquals(resList.get(3), "dddd");
    }

    @Test
    public void testSplitOnPattern() {
        // splitter.onPattern("\\#").trimResults().omitEmptyStrings().splitToList()
        // 用#将分割字符串,转为List集合
        List<String> resList = Splitter.on(Pattern.compile("\\#")).trimResults().
                omitEmptyStrings().splitToList("aaaa  #bbbb  ##cccc#dddd  ");
        Assert.assertNotNull(resList);
        Assert.assertEquals(resList.size(), 4);
        Assert.assertEquals(resList.get(0), "aaaa");
        Assert.assertEquals(resList.get(1), "bbbb");
        Assert.assertEquals(resList.get(2), "cccc");
        Assert.assertEquals(resList.get(3), "dddd");
    }

    @Test
    public void testSplitOnToMap() {
        // Splitter.on(Pattern.compile("\\#")).trimResults().omitEmptyStrings().withKeyValueSeparator("->").split();
        // 用#将分割字符串,转为用->分割的Map集合
        Map<String, String> stringMap = Splitter.on(Pattern.compile("\\#")).trimResults().
                omitEmptyStrings().withKeyValueSeparator("->").split("AAAA->aaaa  #BBBB->bbbb  #");
        Assert.assertNotNull(stringMap);
        Assert.assertEquals(stringMap.size(), 2);
        Assert.assertEquals(stringMap.get("AAAA"), "aaaa");
        Assert.assertEquals(stringMap.get("BBBB"), "bbbb");
    }
}

 

发布了13 篇原创文章 · 获赞 0 · 访问量 188
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览