1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public void stringUtilsDemo() { System.out.println("**StringUtilsDemo**"); System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接."); String[] header = new String[3]; header[0] = StringUtils.repeat("*", 50); header[1] = StringUtils.center(" StringUtilsDemo ", 50, "^O^"); header[2] = header[0]; String head = StringUtils.join(header, "/n"); System.out.println(head);
System.out.println("缩短到某长度,用...结尾."); System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 10)); System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 15, 10));
System.out.println("返回两字符串不同处索引号."); System.out.println(StringUtils.indexOfDifference("aaabc", "aaacc"));
System.out.println("返回两字符串不同处开始至结束."); System.out.println(StringUtils.difference("aaabcde", "aaaccde"));
System.out.println("检查一字符串是否为另一字符串的子集."); System.out.println(StringUtils.containsOnly("aad", "aadd"));
System.out.println("检查一字符串是否不是另一字符串的子集."); System.out.println(StringUtils.containsNone("defg", "aadd"));
System.out.println("检查一字符串是否包含另一字符串."); System.out.println(StringUtils.contains("defg", "ef")); System.out.println(StringUtils.containsOnly("ef", "defg"));
System.out.println("返回可以处理null的toString()."); System.out.println(StringUtils.defaultString("aaaa")); System.out.println("?" + StringUtils.defaultString(null) + "!");
System.out.println("去除字符中的空格."); System.out.println(StringUtils.deleteWhitespace("aa bb cc"));
System.out.println("判断是否是某类字符."); System.out.println(StringUtils.isAlpha("ab")); System.out.println(StringUtils.isAlphanumeric("12")); System.out.println(StringUtils.isBlank("")); System.out.println(StringUtils.isNumeric("123")); System.out.println("左边加字符, 总长度为指定的长度 20, 格式化输出时用"); System.out.println(StringUtils.leftPad("道格拉斯二狗", 20, " ")); }
|