📄 stringutils.java
字号:
int pos = str.indexOf(repl); // If no replacement needed, return the original string // and save StringBuffer allocation/char copying if (pos < 0) { return str; } int len = repl.length(); int lendiff = with.length() - repl.length(); StringBuffer out = new StringBuffer((lendiff <= 0) ? str.length() : (str.length() + (10 * lendiff))); for (; pos >= 0; pos = str.indexOf(repl, lastindex = pos + len)) { out.append(substring(str, lastindex, pos)).append(with); } return out.append(substring(str, lastindex, str.length())).toString(); } public static String replace(String str, char repl, String with) { int pos = str.indexOf(repl); // If no replacement needed, return the original string // and save StringBuffer allocation/char copying if (pos < 0) { return str; } int len = str.length(); int lendiff = with.length() - 1; StringBuffer out = new StringBuffer((lendiff <= 0) ? str.length() : (str.length() + (10 * lendiff))); int lastindex = 0; for (; pos >= 0; pos = str.indexOf(repl, lastindex = pos + 1)) { out.append(substring(str, lastindex, pos)).append(with); } return out.append(substring(str, lastindex, len)).toString(); } public static StringBuffer replace( StringBuffer out, String s, String repl, String with) { int lastindex = 0; int len = repl.length(); for (int index = s.indexOf(repl); index >= 0; index = s.indexOf(repl, lastindex = index + len)) { // we have search string at position index out.append(substring(s, lastindex, index)).append(with); } return out.append(substring(s, lastindex, len)); } /** * Split a string into an array of strings arround a character separator. * This function will be efficient for longer strings * * @param str the string to be split * @param separator the separator character * * @return array of string subparts */ public static String[] splitLongString(String str, char separator) { int len; if (str == null || (len = str.length()) == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } int oldPos = 0; ArrayList list = new ArrayList(); for ( int pos = str.indexOf(separator); pos >= 0; pos = str.indexOf(separator, (oldPos = (pos + 1)))) { list.add(substring(str, oldPos, pos)); } list.add(substring(str, oldPos, len)); return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY); } /** * Split a string into an array of strings arround a character separator. * Each element can be optionally quoted by the quote character.<br> * This function will be efficient for long strings * * @param str the string to be split * @param separator the separator character * @param quote the quote character * * @return array of string subparts * * @throws IllegalArgumentException DOCUMENT ME! */ public static String[] splitLongString( String str, char separator, char quote) { int len; if (str == null || (len = str.length()) == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } int oldPos = 0; ArrayList list = new ArrayList(); for (int pos = 0; pos < len; oldPos = ++pos) { // Skip quoted text, if any while ((pos < len) && (str.charAt(pos) == quote)) { pos = str.indexOf(quote, pos + 1) + 1; if (pos == 0) { throw new IllegalArgumentException( "Closing quote missing in string '" + str + "'"); } } boolean quoted; if (pos != oldPos) { quoted = true; if ((pos < len) && (str.charAt(pos) != separator)) { throw new IllegalArgumentException( "Separator must follow closing quote in string '" + str + "'"); } } else { quoted = false; pos = str.indexOf(separator, pos); if (pos < 0) { pos = len; } } list.add( quoted ? dequote(str, oldPos + 1, pos - 1, quote) : substring(str, oldPos, pos)); } return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY); } /** * Split a string into an array of strings arround a character separator. * This function will be efficient for short strings, for longer strings, * another approach may be better * * @param str the string to be split * @param separator the separator character * * @return array of string subparts */ public static String[] splitShortString(String str, char separator) { int len; if (str == null || (len = str.length()) == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } int lastTokenIndex = 0; // Step 1: how many substrings? // We exchange double scan time for less memory allocation for (int pos = str.indexOf(separator); pos >= 0; pos = str.indexOf(separator, pos + 1)) { lastTokenIndex++; } // Step 2: allocate exact size array String[] list = new String[lastTokenIndex + 1]; int oldPos = 0; // Step 3: retrieve substrings for ( int pos = str.indexOf(separator), i = 0; pos >= 0; pos = str.indexOf(separator, (oldPos = (pos + 1)))) { list[i++] = substring(str, oldPos, pos); } list[lastTokenIndex] = substring(str, oldPos, len); return list; } /** * Split a string into an array of strings arround a character separator. * Each element can be optionally quoted by the quote character.<br> * This function will be efficient for short strings, for longer strings, * another approach may be better * * @param str the string to be split * @param separator the separator character * @param quote the quote character * * @return array of string subparts * * @throws IllegalArgumentException DOCUMENT ME! */ public static String[] splitShortString( String str, char separator, char quote) { int len; if (str == null || (len = str.length()) == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } // Step 1: how many substrings? // We exchange double scan time for less memory allocation int tokenCount = 0; for (int pos = 0; pos < len; pos++) { tokenCount++; int oldPos = pos; // Skip quoted text, if any while ((pos < len) && (str.charAt(pos) == quote)) { pos = str.indexOf(quote, pos + 1) + 1; // pos == 0 is not found (-1 returned by indexOf + 1) if (pos == 0) { throw new IllegalArgumentException( "Closing quote missing in string '" + str + "'"); } } if (pos != oldPos) { if ((pos < len) && (str.charAt(pos) != separator)) { throw new IllegalArgumentException( "Separator must follow closing quote in strng '" + str + "'"); } } else { pos = str.indexOf(separator, pos); if (pos < 0) { break; } } } // Main loop will finish one substring short when last char is separator if (str.charAt(len - 1) == separator) { tokenCount++; } // Step 2: allocate exact size array String[] list = new String[tokenCount]; // Step 3: retrieve substrings // Note: on this pass we do not check for correctness, // since we have already done so tokenCount--; // we want to stop one token short int oldPos = 0; for (int pos = 0, i = 0; i < tokenCount; i++, oldPos = ++pos) { boolean quoted; // Skip quoted text, if any while (str.charAt(pos) == quote) { pos = str.indexOf(quote, pos + 1) + 1; } if (pos != oldPos) { quoted = true; if (str.charAt(pos) != separator) { throw new IllegalArgumentException( "Separator must follow closing quote in strng '" + str + "'"); } } else { quoted = false; pos = str.indexOf(separator, pos); } list[i] = quoted ? dequote(str, oldPos + 1, pos - 1, quote) : substring(str, oldPos, pos); } list[tokenCount] = dequoteFull(str, oldPos, len, quote); return list; } public static String substring(String str, int begin, int end) { if (begin == end) { return ""; } return str.substring(begin, end); } public static String[] trim(String[] strings) { if (strings == null) { return null; } for (int i = 0, len = strings.length; i < len; i++) { strings[i] = strings[i].trim(); } return strings; } /** * Returns the minimum index >= 0, if any * * <p> * Use to find the first of two characters in a string:<br> * <code>minIndex(s.indexOf('/'), indexOf('\'))</code> * </p> * */ public static int minIndex(int a, int b) { return (a < 0) ? b : (b < 0) ? a : (a < b) ? a : b; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -