import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.fileupload.util.Closeable; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.CharSetUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.lang3.time.StopWatch; public class TestStr {
public static void Commons(){ System.out.println(StringUtils.isEmpty(null)); System.out.println(StringUtils.isBlank(" ")); System.out.println(StringUtils.isNotBlank(null)); System.out.println(StringUtils.trim(null)); System.out.println(StringUtils.trimToNull("")); System.out.println(StringUtils.trimToEmpty(null)); System.out.println(StringUtils.strip("大家好 啊 \t")); System.out.println(StringUtils.stripToNull(" \t")); System.out.println(StringUtils.stripToEmpty(null)); System.out.println(StringUtils.defaultString(null)); System.out.println(StringUtils.defaultString("", "df")); System.out.println(StringUtils.defaultIfEmpty(null, "sos")); System.out.println(StringUtils.strip("fsfsdf", "f")); System.out.println(StringUtils.stripStart("ddsuuu ", "d")); System.out.println(StringUtils.stripEnd("dabads", "das")); ArrayToList(StringUtils.stripAll(new String[]{" 中华 ", "民 国 ", "共和 "})); ArrayToList(StringUtils.stripAll(new String[]{" 中华 ", "民 国", "国共和国"}, "国")); System.out.println(StringUtils.equals(null, null)); System.out.println(StringUtils.equalsIgnoreCase("abc", "ABc")); System.out.println(StringUtils.indexOf(null, "a")); System.out.println(StringUtils.indexOf("akfekcd中华", "k", 2)); System.out.println(StringUtils.ordinalIndexOf("akfekcd中华", "k", 2)); System.out.println(StringUtils.indexOfIgnoreCase("adfs", "D")); System.out.println(StringUtils.indexOfIgnoreCase("adfs", "a", 3)); System.out.println(StringUtils.lastIndexOf("adfas", "a")); System.out.println(StringUtils.lastIndexOf("d饿abasdafs我", "a", 3)); System.out.println(StringUtils.lastOrdinalIndexOf("yksdfdht", "f", 2)); System.out.println(StringUtils.lastIndexOfIgnoreCase("sdffet", "E")); System.out.println(StringUtils.lastIndexOfIgnoreCase("efefrfs看", "F" , 2)); System.out.println(StringUtils.contains("sdf", "dg")); System.out.println(StringUtils.containsIgnoreCase("sdf", "D")); System.out.println(StringUtils.containsWhitespace(" d")); System.out.println(StringUtils.indexOfAny("absfekf", new String[]{"f", "b"})); System.out.println(StringUtils.indexOfAny("afefes", "e")); System.out.println(StringUtils.containsAny("asfsd", new char[]{'k', 'e', 's'})); System.out.println(StringUtils.containsAny("啡f咖啡", "咖")); System.out.println(StringUtils.indexOfAnyBut("seefaff", "af")); System.out.println(StringUtils.containsOnly("中华华", "华")); System.out.println(StringUtils.containsOnly("中华中", new char[]{'中', '华'})); System.out.println(StringUtils.containsNone("中华华", "国")); System.out.println(StringUtils.containsNone("中华中", new char[]{'中', '达'})); System.out.println(StringUtils.lastIndexOfAny("中国人民共和国", new String[]{"国人", "共和"})); System.out.println(StringUtils.countMatches("中国人民共和中国", "中国")); System.out.println(StringUtils.isAlpha("这是干什么的2")); System.out.println(StringUtils.isAlphaSpace("NBA直播 ")); System.out.println(StringUtils.isAlphanumeric("NBA直播")); System.out.println(StringUtils.isAlphanumericSpace("NBA直播")); System.out.println(StringUtils.isAsciiPrintable("NBA直播")); System.out.println(StringUtils.isNumeric("NBA直播")); System.out.println(StringUtils.isNumericSpace("33 545")); System.out.println(StringUtils.isWhitespace(" ")); System.out.println(StringUtils.isAllLowerCase("kjk33")); System.out.println(StringUtils.isAllUpperCase("KJKJ")); System.out.println(StringUtils.difference("中国加油", "中国人民共和加油")); System.out.println(StringUtils.indexOfDifference("ww.taobao", "www.taobao.com")); System.out.println(StringUtils.indexOfDifference(new String[] {"中国加油", "中国共和", "中国人民"})); System.out.println(StringUtils.getCommonPrefix(new String[] {"中国加油", "中国共和", "中国人民"})); System.out.println(StringUtils.getLevenshteinDistance("中国共和发国人民", "共和国")); System.out.println(StringUtils.startsWith("中国共和国人民", "中国")); System.out.println(StringUtils.startsWithIgnoreCase("中国共和国人民", "中国")); System.out.println(StringUtils.startsWithAny("abef", new String[]{"ge", "af", "ab"})); System.out.println(StringUtils.endsWith("abcdef", "def")); System.out.println(StringUtils.endsWithIgnoreCase("abcdef", "Def")); System.out.println(StringUtils.substring("中国人民", 2)); System.out.println(StringUtils.substring("中国人民共和国", 2, 4)); System.out.println(StringUtils.left("说点什么好呢", 3)); System.out.println(StringUtils.right("说点什么好呢", 3)); System.out.println(StringUtils.mid("说点什么好呢", 3, 2)); System.out.println(StringUtils.substringBefore("说点什么好呢", "好")); System.out.println(StringUtils.substringAfter("说点什么好呢", "点")); System.out.println(StringUtils.substringBeforeLast("说点什么好点呢", "点")); System.out.println(StringUtils.substringAfterLast("说点什么好点呢?", "点")); System.out.println(StringUtils.substringBetween("南非2010世界杯在南非,在南非", "南非")); ArrayToList(StringUtils.substringsBetween("[a][b][c]", "[", "]")); ArrayToList(StringUtils.split("中华 人民 共和")); ArrayToList(StringUtils.split("中华 ,人民,共和", ",")); ArrayToList(StringUtils.split("中华 :人民:共和", ":", 2)); ArrayToList(StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-")); ArrayToList(StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2)); ArrayToList(StringUtils.splitByWholeSeparatorPreserveAllTokens(" ab de fg ", null)); ArrayToList(StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null, 3)); ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ")); ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ", null)); ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ", null, 2)); ArrayToList(StringUtils.splitByCharacterType("AEkjKr i39:。中文")); ArrayToList(StringUtils.splitByCharacterTypeCamelCase("ASFSRules234")); System.out.println(StringUtils.join(getArrayData())); System.out.println(StringUtils.join(getArrayData(), ":")); System.out.println(StringUtils.join(getArrayData(), ":", 1, 3)); System.out.println(StringUtils.join(getListData(), ":")); System.out.println(StringUtils.deleteWhitespace(" s 中 你 4j")); System.out.println(StringUtils.removeStart("www.baidu.com", "www.")); System.out.println(StringUtils.removeStartIgnoreCase("www.baidu.com", "WWW")); System.out.println(StringUtils.removeEnd("www.baidu.com", ".com")); System.out.println(StringUtils.removeEndIgnoreCase("www.baidu.com", ".COM")); System.out.println(StringUtils.remove("www.baidu.com/baidu", "bai")); System.out.println(StringUtils.chomp("abcrabc\r")); System.out.println(StringUtils.chomp("baidu.com", "com")); System.out.println(StringUtils.chop("wwe.baidu")); System.out.println(StringUtils.replaceOnce("www.baidu.com/baidu", "baidu", "hao123")); System.out.println(StringUtils.replace("www.baidu.com/baidu", "baidu", "hao123")); System.out.println(StringUtils.replace("www.baidu.com/baidu", "baidu", "hao123", 1)); System.out.println(StringUtils.replaceEach("www.baidu.com/baidu", new String[]{"baidu", "com"}, new String[]{"taobao", "net"})); System.out.println(StringUtils.replaceEachRepeatedly("www.baidu.com/baidu", new String[]{"baidu", "com"}, new String[]{"taobao", "net"})); System.out.println(StringUtils.replaceChars("www.baidu.com", "bdm", "qo")); System.out.println(StringUtils.overlay("www.baidu.com", "hao123", 4, 9)); System.out.println(StringUtils.repeat("ba", 3)); System.out.println(StringUtils.repeat("ab", "ou", 3)); System.out.println(StringUtils.rightPad("海川", 4)); System.out.println(StringUtils.rightPad("海川", 4, "河流啊")); System.out.println(StringUtils.leftPad("海川", 4)); System.out.println(StringUtils.leftPad("海川", 4, "大家好")); System.out.println(StringUtils.center("海川", 3)); System.out.println(StringUtils.center("海川", 5, "流")); System.out.println(StringUtils.abbreviate("中华人民共和国", 5)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 12, 10)); System.out.println(StringUtils.abbreviateMiddle("abcdef", ".", 4)); System.out.println(StringUtils.capitalize("Ddf")); System.out.println(StringUtils.uncapitalize("DTf")); System.out.println(StringUtils.swapCase("I am JianNanCun, Hello")); System.out.println(StringUtils.reverse("中国人民")); System.out.println(StringUtils.reverseDelimited("中:国:人民", ':')); System.out.println(CharSetUtils.count( "The quick brown fox jumps over the lazy dog.", "aeiou")); System.out.println(CharSetUtils.delete( "The quick brown fox jumps over the lazy dog.", "aeiou")); System.out.println(CharSetUtils.keep( "The quick brown fox jumps over the lazy dog.", "aeiou")); System.out.println(CharSetUtils.squeeze("a bbbbbb c dd", "b d")); Object obj = null; System.out.println(ObjectUtils.defaultIfNull(obj, "空")); Object a = new Object(); Object b = a; Object c = new Object(); System.out.println(ObjectUtils.equals(a, b)); System.out.println(ObjectUtils.equals(a, c)); Date date = new Date(); System.out.println(ObjectUtils.identityToString(date)); System.out.println(date); System.out.println(ObjectUtils.toString(date)); System.out.println(ObjectUtils.toString(null)); System.out.println(date); System.out.println(RandomStringUtils.random(500)); System.out.println(RandomStringUtils.random(5, "abcdefghijk")); System.out.println(RandomStringUtils.random(5, true, false)); System.out.println(RandomStringUtils.random(5, false, true)); System.out.println(ClassUtils.getAllInterfaces(Date.class)); System.out.println(ClassUtils.getAllSuperclasses(Date.class)); System.out.println(ClassUtils.getShortClassName(Date.class)); System.out.println(ClassUtils.getPackageName(Date.class)); System.out.println(ClassUtils.isAssignable(Date.class, Object.class)); System.out.println(ClassUtils.isAssignable(Object.class, Date.class)); System.out.println("html:" + StringEscapeUtils.escapeHtml4(" <html>")); System.out.println("html:"+ StringEscapeUtils.unescapeEcmaScript(" ")); System.out.println(NumberUtils.max(new int[] { 1, 2, 3, 4 })); System.out.println(NumberUtils.isDigits("123.1")); System.out.println(NumberUtils.isNumber("0123.1")); System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd")); System.out.println(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); StopWatch sw = new StopWatch(); sw.start(); for (Iterator iterator = DateUtils.iterator(new Date(), DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) { Calendar cal = (Calendar) iterator.next(); System.out.println(DateFormatUtils.format(cal.getTime(), "yy-MM-dd HH:mm")); } sw.stop(); System.out.println("秒表计时:" + sw.getTime());
}
private static void ArrayToList(String[] str){ System.out.println(Arrays.asList(str) + " 长度:" + str.length); }
private static List getListData(){ List list = new ArrayList(); list.add("1"); list.add(null); list.add("2"); list.add("3"); return list; }
private static String[] getArrayData(){ return (String[]) getListData().toArray(new String[0]); }
public static Collection<?> union(Collection<?> a, Collection<?> b) { return CollectionUtils.union(a, b); } public static Collection<?> intersection(Collection<?> a, Collection<?> b) { return CollectionUtils.intersection(a, b); } public static Collection<?> disjunction(Collection<?> a, Collection<?> b) { return CollectionUtils.disjunction(a, b); } public static Collection<?> subtract(Collection<?> a, Collection<?> b) { return CollectionUtils.subtract(a, b); }
public static byte[] readFileToByteArray(File file) throws IOException { return FileUtils.readFileToByteArray(file); } public static String readFileToString(File file) throws IOException { return FileUtils.readFileToString(file); } public static void writeStringToFile(File file, String data) throws IOException { FileUtils.writeStringToFile(file, data); } public static void copyFile(File srcFile, File desFile) throws IOException { FileUtils.copyFile(srcFile, desFile); } public static void copyFileToDirectory(File srcDir, File destDir) throws IOException { FileUtils.copyDirectory(srcDir, destDir); } public static void copyURLToFile(URL source, File destination) throws IOException { FileUtils.copyURLToFile(source, destination); } public static void delDirectory(File directory) throws IOException { FileUtils.deleteDirectory(directory); } public static void cleanDirectory(File directory) throws IOException { FileUtils.cleanDirectory(directory); }
public static List<String> readLines(InputStream input) throws IOException { return IOUtils.readLines(input); } public static List<String> readLines(Reader reader) throws IOException { return IOUtils.readLines(reader); } public static void writeLines(List<String> lines, File file) throws FileNotFoundException, IOException { IOUtils.writeLines(lines, null, new FileOutputStream(file)); } public static void writeOneLine(List<String> lines, File file) throws IOException { OutputStream outputStream = new FileOutputStream(file, true); IOUtils.writeLines(lines, null, outputStream, "UTF-8"); } public static String toString(InputStream input) throws IOException { return IOUtils.toString(input); }
public static void main(String[] args) { Commons(); } }
|