📄 strings.java
字号:
*/ public static String fit(String in, int length) { return in.length() > length ? in.substring(0,length) : in + padding(length-in.length()); } /** * Returns a string consisting of the specified number of default * separator characters {@link #DEFAULT_SEPARATOR_CHAR}. * * @param length Number of separator characters in returned * string. * @return String of specified number of default separator * characters. */ public static String padding(int length) { StringBuffer sb = new StringBuffer(); padding(sb,length); return sb.toString(); } /** * Append the specified number of default separator characters * {@link #DEFAULT_SEPARATOR_CHAR} to the specified string buffer. * * @param sb String buffer to which to append specified number of * default separator characters. * @param length Number of separator characters to append. */ public static void padding(StringBuffer sb, int length) { for (int i = 0; i < length; ++i) sb.append(DEFAULT_SEPARATOR_CHAR); } /** * Return a string representation of a function applied * to its arguments. Arguments will be converted to * strings and separated with commas. * * @param functionName Name of function. * @param args Arguments to function. * @return String representation of specified function applied to * specified arguments. */ public static String functionArgs(String functionName, Object[] args) { return functionName + functionArgsList(args); } /** * Returns a string representation of the specified array as a * function's argument list. Each object is converted to a * string, and the list of objects is separated by commas, and the * whole is surrounded by round parentheses. * * @param args Objects to represent arguments. * @return String representation of argument list. */ public static String functionArgsList(Object[] args) { return "(" + concatenate(args,",") + ")"; } /** * Returns <code>true</code> if all of the characters in the * specified array are lower case letters. * * @param chars Array of characters to test. * @return <code>true</code> if all of the characters in the * specified array are lower case letters. */ public static boolean allLowerCase(char[] chars) { for (int i = 0; i < chars.length; ++i) if (!Character.isLowerCase(chars[i])) return false; return true; } /** * Returns <code>true</code> if the specified character sequence * contains only lowercase letters. The test is performed by * {@link Character#isLowerCase(char)}. This is the same test as * performed by {@link #allLowerCase(char[])}. * * @param token Token to check. * @return <code>true</code> if token is all lower-case. */ public static boolean allLowerCase(CharSequence token) { int len = token.length(); for (int i=0; i < len; i++) { if (!Character.isLowerCase(token.charAt(i))) return false; } return true; } /** * Returns <code>true</code> if all of the characters in the * specified array are upper case letters. * * @param chars Array of characters to test. * @return <code>true</code> if all of the characters in the * specified array are upper case letters. */ public static boolean allUpperCase(char[] chars) { for (int i = 0; i < chars.length; ++i) if (!Character.isUpperCase(chars[i])) return false; return true; } /** * Returns <code>true</code> if all of the characters in the * specified array are letters. * * @param chars Array of characters to test. * @return <code>true</code> if all of the characters in the * specified array are letters. */ public static boolean allLetters(char[] chars) { for (int i = 0; i < chars.length; ++i) if (!Character.isLetter(chars[i])) return false; return true; } /** * Returns <code>true</code> if all of the characters in the * specified array are punctuation as specified by * {@link Strings#isPunctuation(char)}. * * @param chars Array of characters to test. * @return <code>true</code> if all of the characters in the * specified array are punctuation. */ public static boolean allPunctuation(char[] chars) { for (int i = 0; i < chars.length; ++i) if (!Strings.isPunctuation(chars[i])) return false; return true; } /** * Returns <code>true</code> if all of the characters in the * specified string are punctuation as specified by * {@link Strings#isPunctuation(char)}. * * @param token Token string to test. * @return <code>true</code> if all of the characters in the * specified string are punctuation. */ public static boolean allPunctuation(String token) { for (int i = token.length(); --i >= 0; ) if (!Strings.isPunctuation(token.charAt(i))) return false; return true; } /** * Returns an array of substrings of the specified string, * in order, with divisions before and after any instance * of the specified character. The returned array will always * have at least one element. Elements in the returned array * may be empty. The following examples illustrate this behavior: * * <br/><br/> * <table border="1" cellpadding="5"> * <tr><td><b>Call</b></td><td><b>Result</b></td></tr> * <tr> * <td><code>split("",' ')</code></td> * <td><code>{ "" }</code></td> * </tr> * <tr> * <td><code>split("a",' ')</code></td> * <td><code>{ "a" }</code></td> * </tr> * <tr> * <td><code>split("a b",' ')</code></td> * <td><code>{ "a", "b" }</code></td> * </tr> * <tr> * <td><code>split("aaa bb cccc",' ')</code></td> * <td><code>{ "aaa", "bb", "cccc" }</code></td> * </tr> * <tr> * <td><code>split(" a",' ')</code></td> * <td><code>{ "", "a" }</code></td> * </tr> * <tr> * <td><code>split("a ",' ')</code></td> * <td><code>{ "a", "" }</code></td> * </tr> * <tr> * <td><code>split(" a ",' ')</code></td> * <td><code>{ "", "a", "" }</code></td> * </tr> * </table> * * @param s String to split. * @param c Character on which to split the string. * @return The array of substrings resulting from splitting the * specified string on the specified character. */ public static String[] split(String s, char c) { char[] cs = s.toCharArray(); int tokCount = 1; for (int i = 0; i < cs.length; ++i) if (cs[i] == c) ++tokCount; String[] result = new String[tokCount]; int tokIndex = 0; int start = 0; for (int end = 0; end < cs.length; ++end) { if (cs[end] == c) { result[tokIndex] = new String(cs,start,end-start); ++tokIndex; start = end+1; } } result[tokIndex] = new String(cs,start,cs.length-start); return result; } /** * Returns <code>true</code> if none of the characters in the * specified array are letters or digits. * * @param cs Array of characters to test. * @return <code>true</code> if none of the characters in the * specified array are letters or digits. */ public static boolean allSymbols(char[] cs) { for (int i = 0; i < cs.length; ++i) if (Character.isLetter(cs[i]) || Character.isDigit(cs[i])) return false; return true; } /** * Returns <code>true</code> if at least one of the characters in * the specified array is a digit. * * @param chars Array of characters to test. * @return <code>true</code> if at least one of the characters in * the specified array is a digit. */ public static boolean containsDigits(char[] chars) { for (int i = 0; i < chars.length; ++i) if (Character.isDigit(chars[i])) return true; return false; } /** * Returns <code>true</code> if at least one of the characters in * the specified array is a letter. * * @param chars Array of characters to test. * @return <code>true</code> if at least one of the characters in * the specified array is a letter. */ public static boolean containsLetter(char[] chars) { for (int i = 0; i < chars.length; ++i) if (Character.isLetter(chars[i])) return true; return false; } /** * Returns <code>true</code> if the first character in the * specified array is an upper case letter and all subsequent * characters are lower case letters. * * @param chars Array of characters to test. * @return <code>true</code> if all of the characters in the * specified array are lower case letters. */ public static boolean capitalized(char[] chars) { if (chars.length == 0) return false; if (!Character.isUpperCase(chars[0])) return false; for (int i = 1; i < chars.length; ++i) if (!Character.isLowerCase(chars[i])) return false; return true; } /** * Returns a title-cased version of the specified word, * which involves capitalizing the first character in * the word if it is a letter. * * @param word The word to convert to title case. * @return Title cased version of specified word. */ public static String titleCase(String word) { if (word.length() < 1) return word; if (!Character.isLetter(word.charAt(0))) return word; return Character.toUpperCase(word.charAt(0)) + word.substring(1); } /** * Returns a hexadecimal string-based representation of the * specified byte array. Each byte is converted using {@link * #byteToHex(byte)} and the results are concatenated into * the final string representation. Letter-based digits are * lowercase. * * @param bytes Array of bytes to convert. * @return The hexadecimal string-based representation of the * specified bytes. */ public static String bytesToHex(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; ++i) sb.append(byteToHex(bytes[i])); return sb.toString(); } /** * Converts the specified byte into a two-digit hexadecimal string * representation. The byte is read as an unsigned value using * {@link Math#byteAsUnsigned(byte)}. The result will always be two * characters, even if the unsigned byte value is less than 16. * Letter-based digits are lowercase. * * @param b Byte to convert. * @return Hexadecimal string representation of byte. */ public static String byteToHex(byte b) { String result = Integer.toHexString(Math.byteAsUnsigned(b)); switch (result.length()) { case 0: return "00"; case 1: return "0" + result; case 2: return result; default: break; } String msg = "byteToHex(" + b + ")=" + result; throw new IllegalArgumentException(msg); } /** * Writes an array of strings to a data output stream. This is * done by writing out the length and then writing out the strings * one at a time using the method {@link * DataOutput#writeUTF(String)}. * * @param dataOut Data output stream to which to write the string. * @param strings Array of strings to write. * @throws IOException If there is an I/O exception writing to the * stream. */ public static void writeArrayTo(DataOutput dataOut, String[] strings) throws IOException { int len = strings.length; dataOut.writeInt(len); for (int i = 0; i < len; ++i) dataOut.writeUTF(strings[i]); } /** * Reads an array of strings from a data input stream. See * {@link #writeArrayTo(DataOutput,String[])} for * information on the encoding used. * * @param dataIn Data input stream from which array is read. * @throws IOException If there is an I/O exception reading from * the stream. */ public static String[] readArrayFrom(DataInput dataIn) throws IOException { int len = dataIn.readInt(); String[] result = new String[len]; for (int i = 0; i < len; ++i) result[i] = dataIn.readUTF();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -