美文网首页
java基础,取出字符串前后逗号

java基础,取出字符串前后逗号

作者: 墨色尘埃 | 来源:发表于2018-10-31 16:02 被阅读15次

根据","截取字符串长度,for循环

    @Test
    public void testTiii() {
        String a = "";
        String b = "";
        String c = "hhh";

        //第一
        for (int i = 0; i < 2; i++) {
            a += b + ",";
        }

        //第二
        for (int i = 0; i < 2; i++) {
            a += b + ",";
            b = "ddsd";
        }
        System.out.println("a=" + a);

        //第三
        for (int i = 0; i < 2; i++) {
            a += c + ",";
        }
        System.out.println("a=" + a);


        for (int i = 0; i < a.split(",").length; i++) {
            if (a.startsWith(",")) {
                a = a.substring(1,a.length());
            }
            if (a.endsWith(",")) {
                a = a.substring(0, a.length() - 1);
            }
        }

        System.out.println("a=" + a);

    }

相关文章

网友评论

      本文标题:java基础,取出字符串前后逗号

      本文链接:https://www.haomeiwen.com/subject/kematqtx.html