📄 stringutils.java
字号:
&& contextMarker == Character.MIN_VALUE) { markerTypeFound = ind; contextMarker = c; } else if (c == target.charAt(0) && !escaped && contextMarker == Character.MIN_VALUE) { if (indexOfIgnoreCase(i, src, target) != -1) return i; } } return -1; } public static int indexOfIgnoreCaseRespectQuotes(int startAt, String src, String target, char quoteChar, boolean allowBackslashEscapes) { char contextMarker = Character.MIN_VALUE; boolean escaped = false; int srcLength = src.length(); for (int i = startAt; i < srcLength; i++) { char c = src.charAt(i); if (allowBackslashEscapes && c == '\\') { escaped = !escaped; } else if (c == contextMarker && !escaped) { contextMarker = Character.MIN_VALUE; } else if (c == quoteChar && !escaped && contextMarker == Character.MIN_VALUE) { contextMarker = c; } else if (c == target.charAt(0) && !escaped && contextMarker == Character.MIN_VALUE) { if (startsWithIgnoreCase(src, i, target)) return i; } } return -1; } /** * Splits stringToSplit into a list, using the given delimitter * * @param stringToSplit * the string to split * @param delimitter * the string to split on * @param trim * should the split strings be whitespace trimmed? * * @return the list of strings, split by delimitter * * @throws IllegalArgumentException * DOCUMENT ME! */ public static final List split(String stringToSplit, String delimitter, boolean trim) { if (stringToSplit == null) { return new ArrayList(); } if (delimitter == null) { throw new IllegalArgumentException(); } StringTokenizer tokenizer = new StringTokenizer(stringToSplit, delimitter, false); List splitTokens = new ArrayList(tokenizer.countTokens()); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (trim) { token = token.trim(); } splitTokens.add(token); } return splitTokens; } /** * Splits stringToSplit into a list, using the given delimitter * * @param stringToSplit * the string to split * @param delimitter * the string to split on * @param trim * should the split strings be whitespace trimmed? * * @return the list of strings, split by delimiter * * @throws IllegalArgumentException * DOCUMENT ME! */ public static final List split(String stringToSplit, String delimiter, String markers, String markerCloses, boolean trim) { if (stringToSplit == null) { return new ArrayList(); } if (delimiter == null) { throw new IllegalArgumentException(); } int delimPos = 0; int currentPos = 0; List splitTokens = new ArrayList(); while ((delimPos = indexOfIgnoreCaseRespectMarker(currentPos, stringToSplit, delimiter, markers, markerCloses, false)) != -1) { String token = stringToSplit.substring(currentPos, delimPos); if (trim) { 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) { if (searchIn == null) { return searchFor == null; } int beginPos = 0; int inLength = searchIn.length(); for (beginPos = 0; 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -