📄 stringutils.java
字号:
/* * Copyright 2004 The Apache Software Foundation. * * 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. */package org.apache.myfaces.util;import java.util.ArrayList;/** * Implements utility functions for the String class * * <p> * Emphasis on performance and reduced memory allocation/garbage collection * in exchange for longer more complex code. * </p> * * @author Anton Koinov (latest modification by $Author: matze $) * @version $Revision: 1.4 $ $Date: 2004/10/13 11:51:01 $ * * $Log: StringUtils.java,v $ * Revision 1.4 2004/10/13 11:51:01 matze * renamed packages to org.apache * * Revision 1.3 2004/08/23 05:15:32 dave0000 * be user friendly, do not return null * * Revision 1.2 2004/08/20 07:16:09 manolito * moved StringUtils to share source tree * * Revision 1.1 2004/08/19 16:18:54 manolito * moved to share dir * * * old cvs log (myfaces dir): * * Revision 1.9 2004/07/01 22:05:15 mwessendorf * ASF switch * * Revision 1.8 2004/06/17 03:51:22 dave0000 * Use EMPTY_STRING_ARRAY from ArrayUtils * * Revision 1.7 2004/03/30 07:42:53 dave0000 * minIndex() * */public class StringUtils{ private StringUtils() { // utility class, no instantiation } //~ Methods ------------------------------------------------------------------------------------ /** * Checks that the string represents a floating point number that CANNOT be * in exponential notation * * @param str the string to check * * @return boolean */ public static boolean isFloatNoExponent(String str) { int len = str.length(); if (len == 0) { return false; } // skip first char if sign char char c = str.charAt(0); int i = ((c == '-') || (c == '+')) ? 1 : 0; // is it only a sign? if (i >= len) { return false; } boolean decimalPointFound = false; do { c = str.charAt(i); if (c == '.') { // is this a second dot? if (decimalPointFound) { return false; } decimalPointFound = true; } else if (!Character.isDigit(c)) { return false; } i++; } while (i < len); return true; } public static boolean isFloatWithOptionalExponent(String str) { int len = str.length(); if (len == 0) { return false; } // skip first char if sign char char c = str.charAt(0); int i = ((c == '-') || (c == '+')) ? 1 : 0; // is it only a sign? if (i >= len) { return false; } boolean exponentFound = false; boolean decimalPointFound = false; do { c = str.charAt(i); switch (c) { case '.': // is this a second one, are we in the exponent? if (decimalPointFound || exponentFound) { return false; } decimalPointFound = true; break; case 'e': case 'E': // is this a second one? if (exponentFound) { return false; } exponentFound = true; // check for exponent sign c = str.charAt(i + 1); if ((c == '-') || (c == '+')) { i++; } break; default: if (!Character.isDigit(c)) { return false; } } i++; } while (i < len); return true; } public static boolean isInteger(String str) { int len = str.length(); if (len == 0) { return false; } // skip first char if sign char char c = str.charAt(0); int i = ((c == '-') || (c == '+')) ? 1 : 0; // is it only a sign? if (i >= len) { return false; } do { if (!Character.isDigit(str.charAt(i))) { return false; } i++; } while (i < len); return true; } public static boolean isUnsignedInteger(String str) { int len = str.length(); if (len == 0) { return false; } for (int i = 0; i < len; i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * Undoubles the quotes inside the string <br> Example:<br> * <pre> * hello""world becomes hello"world * </pre> * * @param str input string to dequote * @param quote the quoting char * * @return dequoted string */ public static String dequote(String str, char quote) { // Is there anything to dequote? if (str == null) { return null; } return dequote(str, 0, str.length(), quote); } /** * Undoubles the quotes inside a substring <br> Example:<br> * <pre> * hello""world becomes hello"world * </pre> * WARNING: scan for quote may continue to the end of the string, make sure * that either <code>charAt(end + 1) == quote</code> or <code>end = * str.lentgth()</code>. If in doubt call * <code>dequote(str.substring(begin, end), quote)</code> * * @param str input string from which to get the substring, must not be * null * @param begin begin index for substring * @param end end index for substring * @param quote the quoting char * * @return dequoted string * * @throws IllegalArgumentException if string is incorrectly quoted */ public static String dequote(String str, int begin, int end, char quote) { // Is there anything to dequote? if (begin == end) { return ""; } int end_ = str.indexOf(quote, begin); // If no quotes, return the original string // and save StringBuffer allocation/char copying if (end_ < 0) { return str.substring(begin, end); } StringBuffer sb = new StringBuffer(end - begin); int begin_ = begin; // need begin later for (; (end_ >= 0) && (end_ < end); end_ = str.indexOf(quote, begin_ = end_ + 2)) { if (((end_ + 1) >= end) || (str.charAt(end_ + 1) != quote)) { throw new IllegalArgumentException( "Internal quote not doubled in string '" + str.substring(begin, end) + "'"); } sb.append(substring(str, begin_, end_)).append(quote); } return sb.append(substring(str, begin_, end)).toString(); } /** * Removes the surrounding quote and any double quote inside the string <br> * Example:<br> * <pre> * "hello""world" becomes hello"world * </pre> * * @param str input string to dequote * @param quote the quoting char * * @return dequoted String */ public static String dequoteFull(String str, char quote) { if (str == null) { return null; } return dequoteFull(str, 0, str.length(), quote); } public static String dequoteFull(String str, int begin, int end, char quote) { // If empty substring, return empty string if (begin == end) { return ""; } // If not quoted, return string if (str.charAt(begin) != quote) { return str.substring(begin, end); } int _end = end - 1; if ((str.length() < 2) || (str.charAt(_end) != quote)) { throw new IllegalArgumentException( "Closing quote missing in string '" + substring(str, begin, end) + "'"); } return dequote(str, begin + 1, _end, quote); } public static String replace(String str, String repl, String with) { int lastindex = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -