欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

内容带分隔符的字符串分隔处理

程序员文章站 2022-04-15 19:37:52
...
我希望使用“;”字符分隔一个字符串的不同部分,如果字符串中包含;,使用\;进行转义,如:
引用
“aaa;” “b;b” “ccc”

这两个字符串连接起来即为:
引用
aaa\;;b\;b;ccc


StringSpliter指定分隔符和转义符,可将这样的字符串分隔出来:

public class StringSpliter {

    public static List<String> spliter(String content, char spliter, char escape) {
        if (!StringUtils.hasText(content)) {
            return Collections.EMPTY_LIST;
        } else {
            boolean hasPerEscape = false;

            List<String> items = Lists.newArrayList();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < content.length(); i++) {
                char c = content.charAt(i);
                if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
                    if (hasPerEscape) {
                        sb.append(escape);
                        hasPerEscape = false;
                    }
                    continue;
                }

                if (c == escape) {
                    if (hasPerEscape) {
                        hasPerEscape = false;
                        sb.append(c);
                    } else {
                        hasPerEscape = true;
                    }
                } else if (c == spliter) {
                    if (hasPerEscape) {
                        sb.append(c);
                        hasPerEscape = false;
                    } else {
                        if (sb.length() > 0) {
                            items.add(sb.toString());
                            sb.delete(0, sb.length());
                        }
                    }
                } else {
                    sb.append(c);
                }
            }
            if (sb.length() > 0) {
                items.add(sb.toString());
            }
            return items;
        }
    }
}


单元测试:
public class StringSpliterTest {

    @Test
    public void testSplit(){
        String str1 = "aa;bb;cc";

        List<String> items = StringSpliter.spliter(str1, ';', '\\');
        assertNotNull(items);
        assertEquals(items.size(), 3);
        assertEquals(items.get(0), "aa");
        assertEquals(items.get(1),"bb");
        assertEquals(items.get(2),"cc");

        str1 = ";aa;bb;;;cc;";
        items = StringSpliter.spliter(str1, ';', '\\');
        assertNotNull(items);
        assertEquals(items.size(), 3);
        assertEquals(items.get(0), "aa");
        assertEquals(items.get(1),"bb");
        assertEquals(items.get(2),"cc");

        str1 = "a\\;a;bb\\;;\\;cc";
        items = StringSpliter.spliter(str1, ';', '\\');
        assertNotNull(items);
        assertEquals(items.size(), 3);
        assertEquals(items.get(0), "a;a");
        assertEquals(items.get(1),"bb;");
        assertEquals(items.get(2),";cc");

        str1 = "\\;;\\;";
        items = StringSpliter.spliter(str1, ';', '\\');
        assertNotNull(items);
        assertEquals(items.size(), 2);
        assertEquals(items.get(0), ";");
        assertEquals(items.get(1),";");

        str1 = ";;;;;";
        items = StringSpliter.spliter(str1, ';', '\\');
        assertNotNull(items);
        assertEquals(items.size(), 0);

        str1 = "\\ ; \\ ;";
        items = StringSpliter.spliter(str1, ';', '\\');
        assertNotNull(items);
        assertEquals(items.size(), 2);
        assertEquals(items.get(0), "\\");
        assertEquals(items.get(1),"\\");

        str1 = "aaa";
        items = StringSpliter.spliter(str1, ';', '\\');
        assertNotNull(items);
        assertEquals(items.size(), 1);
        assertEquals(items.get(0), "aaa");
    }
}