1 16 package scriptella.util; 17 18 import java.util.regex.Pattern ; 19 20 26 public final class StringUtils { 27 private StringUtils() { } 29 30 36 public static boolean isEmpty(final CharSequence cs) { 37 return cs == null || cs.length() == 0; 38 } 39 40 41 47 public static String nullsafeTrim(final CharSequence cs) { 48 return cs == null ? "" : cs.toString().trim(); 49 } 50 51 56 public static String nullsafeToString(final Object o) { 57 return o==null?"":o.toString(); 58 } 59 60 66 public static boolean isAsciiWhitespacesOnly(final CharSequence cs) { 67 if (cs == null) { 68 return true; 69 } 70 int len = cs.length(); 71 if (len == 0) { 72 return true; 73 } 74 for (int i = 0; i < len; i++) { 75 if (cs.charAt(i) > ' ') { 76 return false; 77 } 78 } 79 return true; 80 } 81 82 88 public static boolean isDecimalInt(final CharSequence cs) { 89 if (cs == null) { return false; 91 } 92 int len = cs.length(); 93 if (len == 0) { return false; 95 } 96 for (int i = 0; i < len; i++) { 97 int c = cs.charAt(i); 98 if (c < 0x30 || c > 0x39) { 99 return false; 100 } 101 } 102 return true; 103 } 104 105 private static Pattern WHITESPACES = Pattern.compile("[\\x00-\\x20&&[^\\r\\n]]+"); 106 private static Pattern EOLS = Pattern.compile("[\\r\\n]+"); 107 108 116 public static String consoleFormat(String string) { 117 if (string == null) { 118 return ""; 119 } 120 String res = string.trim(); 121 String sep = System.getProperty("line.separator"); 122 if (sep == null) { 123 sep = "\n"; 124 } 125 res = EOLS.matcher(res).replaceAll(sep); 126 res = WHITESPACES.matcher(res).replaceAll(" "); 127 return res; 128 } 129 130 131 } 132 | Popular Tags |