📄 stringutils.java
字号:
token = token.trim(); } splitTokens.add(token); currentPos = delimPos + 1; } if (currentPos < stringToSplit.length()) { String token = stringToSplit.substring(currentPos); if (trim) { token = token.trim(); } splitTokens.add(token); } return splitTokens; } private static boolean startsWith(byte[] dataFrom, String chars) { for (int i = 0; i < chars.length(); i++) { if (dataFrom[i] != chars.charAt(i)) { return false; } } return true; } /** * Determines whether or not the string 'searchIn' contains the string * 'searchFor', dis-regarding case starting at 'startAt' Shorthand for a * String.regionMatch(...) * * @param searchIn * the string to search in * @param startAt * the position to start at * @param searchFor * the string to search for * * @return whether searchIn starts with searchFor, ignoring case */ public static boolean startsWithIgnoreCase(String searchIn, int startAt, String searchFor) { return searchIn.regionMatches(true, startAt, searchFor, 0, searchFor .length()); } /** * Determines whether or not the string 'searchIn' contains the string * 'searchFor', dis-regarding case. Shorthand for a String.regionMatch(...) * * @param searchIn * the string to search in * @param searchFor * the string to search for * * @return whether searchIn starts with searchFor, ignoring case */ public static boolean startsWithIgnoreCase(String searchIn, String searchFor) { return startsWithIgnoreCase(searchIn, 0, searchFor); } /** * Determines whether or not the sting 'searchIn' contains the string * 'searchFor', disregarding case,leading whitespace and non-alphanumeric * characters. * * @param searchIn * the string to search in * @param searchFor * the string to search for * * @return true if the string starts with 'searchFor' ignoring whitespace */ public static boolean startsWithIgnoreCaseAndNonAlphaNumeric( String searchIn, String searchFor) { if (searchIn == null) { return searchFor == null; } int beginPos = 0; int inLength = searchIn.length(); for (beginPos = 0; beginPos < inLength; beginPos++) { char c = searchIn.charAt(beginPos); if (Character.isLetterOrDigit(c)) { break; } } return startsWithIgnoreCase(searchIn, beginPos, searchFor); } /** * Determines whether or not the sting 'searchIn' contains the string * 'searchFor', disregarding case and leading whitespace * * @param searchIn * the string to search in * @param searchFor * the string to search for * * @return true if the string starts with 'searchFor' ignoring whitespace */ public static boolean startsWithIgnoreCaseAndWs(String searchIn, String searchFor) { return startsWithIgnoreCaseAndWs(searchIn, searchFor, 0); } /** * Determines whether or not the sting 'searchIn' contains the string * 'searchFor', disregarding case and leading whitespace * * @param searchIn * the string to search in * @param searchFor * the string to search for * @param beginPos * where to start searching * * @return true if the string starts with 'searchFor' ignoring whitespace */ public static boolean startsWithIgnoreCaseAndWs(String searchIn, String searchFor, int beginPos) { if (searchIn == null) { return searchFor == null; } int inLength = searchIn.length(); for (; beginPos < inLength; beginPos++) { if (!Character.isWhitespace(searchIn.charAt(beginPos))) { break; } } return startsWithIgnoreCase(searchIn, beginPos, searchFor); } /** * @param bytesToStrip * @param prefix * @param suffix * @return */ public static byte[] stripEnclosure(byte[] source, String prefix, String suffix) { if (source.length >= prefix.length() + suffix.length() && startsWith(source, prefix) && endsWith(source, suffix)) { int totalToStrip = prefix.length() + suffix.length(); int enclosedLength = source.length - totalToStrip; byte[] enclosed = new byte[enclosedLength]; int startPos = prefix.length(); int numToCopy = enclosed.length; System.arraycopy(source, startPos, enclosed, 0, numToCopy); return enclosed; } return source; } /** * Returns the bytes as an ASCII String. * * @param buffer * the bytes representing the string * * @return The ASCII String. */ public static final String toAsciiString(byte[] buffer) { return toAsciiString(buffer, 0, buffer.length); } /** * Returns the bytes as an ASCII String. * * @param buffer * the bytes to convert * @param startPos * the position to start converting * @param length * the length of the string to convert * * @return the ASCII string */ public static final String toAsciiString(byte[] buffer, int startPos, int length) { char[] charArray = new char[length]; int readpoint = startPos; for (int i = 0; i < length; i++) { charArray[i] = (char) buffer[readpoint]; readpoint++; } return new String(charArray); } /** * Compares searchIn against searchForWildcard with wildcards (heavily * borrowed from strings/ctype-simple.c in the server sources) * * @param searchIn * the string to search in * @param searchForWildcard * the string to search for, using the 'standard' SQL wildcard * chars of '%' and '_' * * @return WILD_COMPARE_MATCH_NO_WILD if matched, WILD_COMPARE_NO_MATCH if * not matched with wildcard, WILD_COMPARE_MATCH_WITH_WILD if * matched with wildcard */ public static int wildCompare(String searchIn, String searchForWildcard) { if ((searchIn == null) || (searchForWildcard == null)) { return WILD_COMPARE_NO_MATCH; } if (searchForWildcard.equals("%")) { //$NON-NLS-1$ return WILD_COMPARE_MATCH_WITH_WILD; } int result = WILD_COMPARE_NO_MATCH; /* Not found, using wildcards */ char wildcardMany = '%'; char wildcardOne = '_'; char wildcardEscape = '\\'; int searchForPos = 0; int searchForEnd = searchForWildcard.length(); int searchInPos = 0; int searchInEnd = searchIn.length(); while (searchForPos != searchForEnd) { char wildstrChar = searchForWildcard.charAt(searchForPos); while ((searchForWildcard.charAt(searchForPos) != wildcardMany) && (wildstrChar != wildcardOne)) { if ((searchForWildcard.charAt(searchForPos) == wildcardEscape) && ((searchForPos + 1) != searchForEnd)) { searchForPos++; } if ((searchInPos == searchInEnd) || (Character.toUpperCase(searchForWildcard .charAt(searchForPos++)) != Character .toUpperCase(searchIn.charAt(searchInPos++)))) { return WILD_COMPARE_MATCH_WITH_WILD; /* No match */ } if (searchForPos == searchForEnd) { return ((searchInPos != searchInEnd) ? WILD_COMPARE_MATCH_WITH_WILD : WILD_COMPARE_MATCH_NO_WILD); /* * Match if both are * at end */ } result = WILD_COMPARE_MATCH_WITH_WILD; /* Found an anchor char */ } if (searchForWildcard.charAt(searchForPos) == wildcardOne) { do { if (searchInPos == searchInEnd) { /* * Skip one char if * possible */ return (result); } searchInPos++; } while ((++searchForPos < searchForEnd) && (searchForWildcard.charAt(searchForPos) == wildcardOne)); if (searchForPos == searchForEnd) { break; } } if (searchForWildcard.charAt(searchForPos) == wildcardMany) { /* * Found * w_many */ char cmp; searchForPos++; /* Remove any '%' and '_' from the wild search string */ for (; searchForPos != searchForEnd; searchForPos++) { if (searchForWildcard.charAt(searchForPos) == wildcardMany) { continue; } if (searchForWildcard.charAt(searchForPos) == wildcardOne) { if (searchInPos == searchInEnd) { return (WILD_COMPARE_NO_MATCH); } searchInPos++; continue; } break; /* Not a wild character */ } if (searchForPos == searchForEnd) { return WILD_COMPARE_MATCH_NO_WILD; /* Ok if w_many is last */ } if (searchInPos == searchInEnd) { return WILD_COMPARE_NO_MATCH; } if (((cmp = searchForWildcard.charAt(searchForPos)) == wildcardEscape) && ((searchForPos + 1) != searchForEnd)) { cmp = searchForWildcard.charAt(++searchForPos); } searchForPos++; do { while ((searchInPos != searchInEnd) && (Character.toUpperCase(searchIn .charAt(searchInPos)) != Character .toUpperCase(cmp))) searchInPos++; if (searchInPos++ == searchInEnd) { return WILD_COMPARE_NO_MATCH; } { int tmp = wildCompare(searchIn, searchForWildcard); if (tmp <= 0) { return (tmp); } } } while ((searchInPos != searchInEnd) && (searchForWildcard.charAt(0) != wildcardMany)); return WILD_COMPARE_NO_MATCH; } } return ((searchInPos != searchInEnd) ? WILD_COMPARE_MATCH_WITH_WILD : WILD_COMPARE_MATCH_NO_WILD); } static byte[] s2b(String s, ConnectionImpl conn) throws SQLException { if (s == null) { return null; } if ((conn != null) && conn.getUseUnicode()) { try { String encoding = conn.getEncoding(); if (encoding == null) { return s.getBytes(); } SingleByteCharsetConverter converter = conn .getCharsetConverter(encoding); if (converter != null) { return converter.toBytes(s); } return s.getBytes(encoding); } catch (java.io.UnsupportedEncodingException E) { return s.getBytes(); } } return s.getBytes(); } public static int lastIndexOf(byte[] s, char c) { if (s == null) { return -1; } for (int i = s.length - 1; i >= 0; i--) { if (s[i] == c) { return i; } } return -1; } public static int indexOf(byte[] s, char c) { if (s == null) { return -1; } int length = s.length; for (int i = 0; i < length; i++) { if (s[i] == c) { return i; } } return -1; } public static boolean isNullOrEmpty(String toTest) { return (toTest == null || toTest.length() == 0); } /** * Returns the given string, with comments removed * * @param src * the source string * @param stringOpens * characters which delimit the "open" of a string * @param stringCloses * characters which delimit the "close" of a string, in * counterpart order to <code>stringOpens</code> * @param slashStarComments * strip slash-star type "C" style comments * @param slashSlashComments * strip slash-slash C++ style comments to end-of-line * @param hashComments * strip #-style comments to end-of-line * @param dashDashComments * strip "--" style comments to end-of-line * @return the input string with all comment-delimited data removed */ public static String stripComments(String src, String stringOpens, String stringCloses, boolean slashStarComments, boolean slashSlashComments, boolean hashComments, boolean dashDashComments) { if (src == null) { return null; } StringBuffer buf = new StringBuffer(src.length()); // It's just more natural to deal with this as a stream // when parsing..This code is currently only called when // parsing the kind of metadata that developers are strongly // recommended to cache anyways, so we're not worried // about the _1_ extra object allocation if it cleans // up the code StringReader sourceReader = new StringReader(src); int contextMarker = Character.MIN_VALUE; boolean escaped = false; int markerTypeFound = -1; int ind = 0; int currentChar = 0; try { while ((currentChar = sourceReader.read()) != -1) { if (false && currentChar == '\\') { escaped = !escaped; } else if (markerTypeFound != -1 && currentChar == stringCloses.charAt(markerTypeFound) && !escaped) { contextMarker = Character.MIN_VALUE; markerTypeFound = -1; } else if ((ind = stringOpens.indexOf(currentChar)) != -1 && !escaped && contextMarker == Character.MIN_VALUE) { markerTypeFound = ind; contextMarker = currentChar; } if (contextMarker == Character.MIN_VALUE && currentChar == '/' && (slashSlashComments || slashStarComments)) { currentChar = sourceReader.read(); if (currentChar == '*' && slashStarComments) { int prevChar = 0; while ((currentChar = sourceReader.read()) != '/' || prevChar != '*') { if (currentChar == '\r') { currentChar = sourceReader.read(); if (currentChar == '\n') { currentChar = sourceReader.read(); } } else { if (currentChar == '\n') { currentChar = sourceReader.read(); } } if (currentChar < 0) break; prevChar = currentChar; } continue; } else if (currentChar == '/' && slashSlashComments) { while ((currentChar = sourceReader.read()) != '\n' && currentChar != '\r' && currentChar >= 0) ; } } else if (contextMarker == Character.MIN_VALUE && currentChar == '#' && hashComments) { // Slurp up everything until the newline while ((currentChar = sourceReader.read()) != '\n' && currentChar != '\r' && currentChar >= 0) ; } else if (contextMarker == Character.MIN_VALUE && currentChar == '-' && dashDashComments) { currentChar = sourceReader.read(); if (currentChar == -1 || currentChar != '-') { buf.append('-'); if (currentChar != -1) { buf.append(currentChar); } continue; } // Slurp up everything until the newline while ((currentChar = sourceReader.read()) != '\n' && currentChar != '\r' && currentChar >= 0) ; } if (currentChar != -1) { buf.append((char) currentChar); } } } catch (IOException ioEx) { // we'll never see this from a StringReader } return buf.toString(); } public static final boolean isEmptyOrWhitespaceOnly(String str) { if (str == null || str.length() == 0) { return true; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -