📄 stringtools.java
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description:// This class provides many String based utilities.// ----------------------------------------------------------------------------// Change History:// 2006/03/26 Martin D. Flynn// -Initial release// 2006/04/11 Martin D. Flynn// -Decimal formatting now explicitly uses "Locale.US" symbols. This fixes // a problem that caused values such as Latitude "37,1234" from appearing // in CSV files.// 2006/05/11 Martin D. Flynn// -Replaced deprecated 'Character.isSpace' with 'Character.isWhitespace'// 2006/06/30 Martin D. Flynn// -Repackaged// 2007/02/25 Martin D. Flynn// -Added 'isDouble', 'isFloat', 'isInt', 'isLong', 'isBoolean'// -Fixed negative case in 'parseBoolean'.// -Removed "on"/"off" from valid boolean string values.// 2007/07/27 Martin D. Flynn// -Added support for encoding primitive arrays// 2007/09/16 Martin D. Flynn// -Fixed 'insertKeyValues' to properly account for the length of the beginning// and ending delimiter lengths.// 2008/02/04 Martin D. Flynn// -Added 'parseFloat'/'parseDouble' method for decoding 32-bit and 64-bit // IEEE 754 floating-point values.// -Validate 'blockLen' argument in 'formatHexString'.// 2008/02/27 Martin D. Flynn// -Added date formatting to 'format(...)'// 2008/03/28 Martin D. Flynn// -Added additional support for character encoding// 2008/04/11 Martin D. Flynn// -Added method 'endsWithIgnoreCase'// 2008/05/14 Martin D. Flynn// -Added methods 'escapeUnicode', 'unescapeUnicode', 'trim', 'isBlank'// ----------------------------------------------------------------------------package org.opengts.util;import java.io.*;import java.util.*;import java.util.regex.*;import java.text.*;import java.math.*;import java.awt.*;import java.lang.reflect.Array;public class StringTools{ // ------------------------------------------------------------------------ public static final char BACKSPACE = '\b'; public static final char FORM_FEED = '\f'; public static final char NEW_LINE = '\n'; public static final char CARRIAGE_RETURN = '\r'; public static final char TAB = '\t'; public static final String WhitespaceChars = " \t\b\f\r\n"; public static final char KeyValSeparatorChar = '='; // "=" public static final char PropertySeparatorChar = ' '; // ';' // ------------------------------------------------------------------------ private static final String BooleanTRUE[] = { "true" , "yes", "1" }; // must be lower-case private static final String BooleanFALSE[] = { "false", "no" , "0" }; // must be lower-case // ------------------------------------------------------------------------ public static final String FORMAT_TIME = "time"; // ------------------------------------------------------------------------ public static final String CharEncoding_UTF_8 = "UTF-8"; public static final String CharEncoding_UTF_16 = "UTF-16"; public static final String CharEncoding_8859_1 = "ISO-8859-1"; private static final String DefaultCharEncoding = CharEncoding_8859_1; // ------------------------------------------------------------------------ private static String charEncoding = DefaultCharEncoding; /** *** Sets the character encoding used to encode/decode Strings *** @param charEnc The character encoding **/ public static void setCharacterEncoding(String charEnc) { StringTools.charEncoding = !StringTools.isBlank(charEnc)? charEnc : DefaultCharEncoding; } /** *** Gets the character encoding used to encode/decode Strings *** @return The character encoding **/ public static String getCharacterEncoding() { return !StringTools.isBlank(StringTools.charEncoding)? StringTools.charEncoding : DefaultCharEncoding; } /** *** Returns an array of available character encodings *** @return The default character set **/ public static String[] getCharacterEncodings() { Map csMap = java.nio.charset.Charset.availableCharsets(); String charEnc[] = new String[csMap.size()]; int c = 0; for (Iterator i = csMap.keySet().iterator(); i.hasNext();) { charEnc[c++] = (String)i.next(); } return charEnc; } /** *** Returns the default character set *** @return The default character set **/ private static int CHARSET_SOURCE = 2; public static String getDefaultCharacterEncoding() { // References: // - http://blogs.warwick.ac.uk/kieranshaw/entry/utf-8_internationalisation_with/ String charSet = null; switch (CHARSET_SOURCE) { case 0: // not cross-plateform safe charSet = System.getProperty("file.encoding"); // Note: Setting this property will not change the default character encoding for // the current Java process. In order to change the character encoding, this // property must be set on the start-up command line. (IE. "-Dfile.encoding=UTF-8") break; case 1: // JDK1.4 charSet = new java.io.OutputStreamWriter(new java.io.ByteArrayOutputStream()).getEncoding(); break; case 2: // This requires JDK1.5 to compile charSet = java.nio.charset.Charset.defaultCharset().name(); break; } return charSet; } // ------------------------------------------------------------------------ /** *** Return a 'char' array of the specified String *** @param s The String from which the char array will be returned *** @return The array of 'char's from the specified String **/ public static char[] getChars(String s) { return (s != null)? s.toCharArray() : null; } /** *** Converts the specified byte array to an array of chars. *** This method converts a single byte to a single character. Multibyte character *** conversions are not supported. *** @param b The array of bytes to convert to characters *** @return The array of characters created from the byte array **/ public static char[] getChars(byte b[]) { if (b != null) { char c[] = new char[b.length]; for (int i = 0; i < b.length; i++) { c[i] = (char)((int)b[i] & 0xFF); } return c; } else { return null; } } // ------------------------------------------------------------------------ /** *** Converts the String represented by the specified StringBuffer into a byte *** array based on the default character set (see <code>StringTools.getCharacterEncoding()</code>) *** @param sb The StringBuffer which will be converted to a byte array *** @return The byte array representation of the StringBuffer **/ public static byte[] getBytes(StringBuffer sb) { return (sb != null)? getBytes(sb.toString()) : null; } /** *** Converts the specified String into a byte array based on the default character *** set (see <code>StringTools.getCharacterEncoding()</code>) *** @param s The String which will be converted to a byte array *** @return The byte array representation of the specified String **/ public static byte[] getBytes(String s) { if (s != null) { try { return s.getBytes(StringTools.getCharacterEncoding()); } catch (UnsupportedEncodingException uce) { // will not occur Print.logStackTrace("Charset not found: " + StringTools.getCharacterEncoding()); return s.getBytes(); } } else { return null; } } /** *** Converts the specified char array into a byte array. Character are converted *** as 1 byte per character. Multibyte conversions are not supported by this method. *** @param c The char array which will be converted to a byte array *** @return The byte array representation of the specified char array **/ public static byte[] getBytes(char c[]) { if (c != null) { byte b[] = new byte[c.length]; for (int i = 0; i < c.length; i++) { b[i] = (byte)c[i]; } return b; } else { return null; } } // ------------------------------------------------------------------------ /** *** Returns true if the specified byte array contains only printable ASCII characters *** @param b The byte array tested for printable ASCII *** @param inclSpace True to include space characters (' ', '\t', '\n', '\r'), false to omit. *** @return True if the specified byte array contains only printable ASCII characters **/ public static boolean isPrintableASCII(byte b[], boolean inclSpace) { // Printable ASCII has a valid range of 33 ('!') to 126 ('~') // Space characters are 9 ('\t'), 10 ('\n'), and 13 ('\r'), and 32 (' ') if ((b == null) || (b.length == 0)) { return false; } else { for (int i = 0; i < b.length; i++) { if ((b[i] < (byte)33) || (b[i] > 126)) { if (!inclSpace) { // outside of acceptable range, and we're not including space characters return false; } else if ((b[i] != ' ') && (b[i] != '\t') && (b[i] != '\n') && (b[i] != '\r')) { // outside of acceptable range, and not a space character return false; } } } // everything checks out return true; } } /** *** Converts the specified byte array to a String based on the default character set, *** replacing any unprintable characters. *** @param b The byte array to convert to a String, based on the default character set. *** @param repUnp The character used to replace any detected unprintable characters. *** @return The String representation of the specified byte array **/ public static String toStringValue(byte b[], char repUnp) { if (b != null) { byte p[] = new byte[b.length]; System.arraycopy(b, 0, p, 0, b.length); for (int i = 0; i < p.length; i++) { if ((p[i] < 32) || (p[i] > 126)) { p[i] = (byte)repUnp; } } return StringTools.toStringValue(p, 0, p.length); } else { return null; } } /** *** Converts the specified byte array to a String based on the default character set. *** @param b The byte array to convert to a String, based on the default character set. *** @return The String representation of the specified byte array **/ public static String toStringValue(byte b[]) { return (b != null)? StringTools.toStringValue(b, 0, b.length) : null; } /** *** Converts the specified byte array to a String based on the default character set. *** @param b The byte array to convert to a String, based on the default character set. *** @param ofs The offset within the byte array to convert to a String *** @param len The number of bytes starting at the specified offset to convert to a String *** @return The String representation of the specified byte array **/ public static String toStringValue(byte b[], int ofs, int len) { if (b != null) { try { return new String(b, ofs, len, StringTools.getCharacterEncoding()); } catch (Throwable t) { // This should NEVER occur (at least not because of the charset) Print.logException("Byte=>String conversion error", t); return new String(b, ofs, len); } } else { return null; // what goes around ... } } /** *** Converts the specified character array to a String. *** @param c The char array to convert to a String *** @return The String representation of the specified char array **/ public static String toStringValue(char c[]) { return new String(c); } // ------------------------------------------------------------------------ /** *** Converts the specified String to a Unicode escaped String.<br> *** That is, convert non-ASCII characters to '\u0000' encoded characters.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -