⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutil.java

📁 google的gdata api包
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
          break;        case '<':          sb.append("&lt;");          break;        case '>':          sb.append("&gt;");          break;        case '\0':          // \0 is not a valid XML char - skip it          break;        default:          sb.append(ch);          break;      }    }    return sb.toString();  }  /**   * Escapes special characters (& < > ") from a string so it can safely be   * included in an HTML document. (same as <code>xmlEscape</code> except that   * <code>htmlEscape</code> does not escape the apostrophe character).   */  public static String htmlEscape(String s) {    // This method gets called A LOT so it has to be excruciatingly efficient.    // Older versions were responsible for several percent of all objects    // created on the heap, and 10% of total execution time.    // In particular, if the String has no characters that need escaping, this    // method should return its argument.    StringBuilder sb = null;    String replacement;    int start = 0; // the earliest input position we haven't copied yet.    for (int i = 0; i < s.length(); i++) {      switch (s.charAt(i)) {        case '"':          replacement = "&quot;";          break;        case '&':          replacement = "&amp;";          break;        case '<':          replacement = "&lt;";          break;        case '>':          replacement = "&gt;";          break;        default:          replacement = null;      }      if (replacement != null) {        if (sb == null) {          // This is the first time we have found a replacement. Allocate the          // StringBuilder now.          // This initial size for the StringBuilder below will be exact if          // this initial replacement is the only one. If not, sb will expand.          sb = new StringBuilder(s.length() + replacement.length() - 1);        }        if (i > start) {          // we have to copy some of the earlier string.          sb.append(s.substring(start, i));        }        sb.append(replacement);        start = i+1;      }    }    // now possibly also copy what's leftover in the input string.    if (start > 0) {      sb.append(s.substring(start));    }    if (sb != null) {      return sb.toString();    }    return s;  }  /**   * Escapes the special characters from a string so it can be used as part of   * a regex pattern. This method is for use on gnu.regexp style regular   * expressions.   */  public static String regexEscape(String s) {    StringBuilder sb = new StringBuilder();    for(int i = 0; i < s.length(); i++) {      char c = s.charAt(i);      // Test if c is an escapable character      if ("()|*+?.{}[]$^\\".indexOf(c) != -1) {        sb.append('\\');        sb.append(c);      }      else {        sb.append(c);      }    }    return sb.toString();  }  /**   * Escapes the special characters from a string so it can be used as part of   * a regex pattern. This method is for use on regexes in the flavor of the   * java.util.regex package. This method should be removed when we move to   * the java version 1.5 (Tiger) release, since that release gives us   * a literal regex flag as well as a quote method to produce literal regexes.   */  public static String javaUtilRegexEscape(String s) {    // Use the quoting mechanism in Pattern unless it is interfered with    // by the contents of the string.    if (s.indexOf("\\E") == -1) {      return "\\Q" + s + "\\E";    }    // Very rare case: must escape each character.    StringBuilder sb = new StringBuilder();    for (int i = 0; i < s.length(); i++) {      sb.append('\\');      sb.append(s.charAt(i));    }    return sb.toString();  }  /**   * Escapes the '\' and '$' characters, which comprise the subset of regex   * characters that has special meaning in methods such as:   *   * <pre>java.util.regex.Matcher.appendReplacement(sb, replacement);</pre>   * <pre>java.lang.String.replaceAll(str, replacement);</pre>   *   * Note that this method is offered in java version 1.5 as the method   *   * <pre>java.util.regex.Matcher.quoteReplacement(String);</pre>   */  public static String regexReplacementEscape(String s) {    StringBuilder sb = null;    for (int i = 0, n = s.length(); i < n; i++) {      char c = s.charAt(i);      switch (c) {        case '\\':        case '$':          if (sb == null) {            // This is the first replacement necessary. Initialize the            // string buffer to contain the all of the previously checked            // characters in 's'            sb = new StringBuilder(s.substring(0, i));          }          sb.append('\\');        default:          if (sb != null) {            sb.append(c);          }          break;      }    }    return (sb == null) ? s : sb.toString();  }  /**   * The old interface to cropBetween - using a single char limit   */  public static String cropBetween(String in, char limit) {    return cropBetween(in, String.valueOf(new char[]{limit}));  }  /**   * This removes characters between maching charLimit chars. For example   * cropBetween("ab^cd^ef^gh^hi", '^') will return "abefhi" It will consider   * squences of 2 charLimit as one charLimit in the output   *   * @param in - the string to process   * @param limit - the limit of the string(s) to remove   *   * @return String - the cropped string   */  public static String cropBetween(String in, String limit) {    StringBuilder out = new StringBuilder();    int lastPos = 0;    int lenLimit = limit.length();    boolean modeAdd = true;    int pos = -1;    while ((pos = in.indexOf(limit, lastPos)) >= 0) {      if (modeAdd) {        out.append(in.substring(lastPos, pos));      }      modeAdd = !modeAdd;      lastPos = pos + lenLimit;    }    // add the remainings    if (modeAdd) {      out.append(in.substring(lastPos));    }    return out.toString();  }  /**   * This converts a String to a list of strings by extracting the substrings   * between delimiter   *   * @param in - what to process   * @param delimiter - the delimiting string   * @param doStrip - to strip the substrings before adding to the list   *   * @return LinkedList   */  public static LinkedList<String> string2List(String in, String delimiter,                                               boolean doStrip) {    if (in == null) {      return null;    }    LinkedList<String> out = new LinkedList<String>();    string2Collection(in, delimiter, doStrip, out);    return out;  }  /**   * This converts a String to a Set of strings by extracting the substrings   * between delimiter   *   * @param in - what to process   * @param delimiter - the delimiting string   * @param doStrip - to strip the substrings before adding to the list   *   * @return Set   */  public static Set string2Set(String in, String delimiter, boolean doStrip) {    if (in == null) {      return null;    }    HashSet<String> out = new HashSet<String>();    string2Collection(in, delimiter, doStrip, out);    return out;  }  /**   * Converts a delimited string to a collection of strings. Substrings between   * delimiters are extracted from the string and added to a collection that is   * provided by the caller.   *   * @param in The delimited input string to process   * @param delimiter The string delimiting entries in the input string.   * @param doString Whether to strip the substrings before adding to the   *          collection   * @param collection The collection to which the strings will be added. If   *          <code>null</code>, a new <code>List</code> will be created.   *   * @return The collection to which the substrings were added. This is   *         syntactic sugar to allow call chaining.   */  public static Collection<String> string2Collection(String in,      String delimiter,      boolean doStrip,      Collection<String> collection) {    if (in == null) {      return null;    }    if (collection == null) {      collection = new ArrayList<String>();    }    if (delimiter == null || delimiter.length() == 0) {      collection.add(in);      return collection;    }    int fromIndex = 0;    int pos;    while ((pos = in.indexOf(delimiter, fromIndex)) >= 0) {      String interim = in.substring(fromIndex, pos);      if (doStrip) {        interim = strip(interim);      }      if (!doStrip || interim.length() > 0) {        collection.add(interim);      }      fromIndex = pos + delimiter.length();    }    String interim = in.substring(fromIndex);    if (doStrip) {      interim = strip(interim);    }    if (!doStrip || interim.length() > 0) {      collection.add(interim);    }    return collection;  }  /**   * Lots of people called list2String when in fact it was implemented as   * Collection2String. I added Collection2String as a new function and am   * leaving the list2String function signature here so it can continue to be   *   * @deprecated Please use   *     But note that {@code Join} does not consider null elements to be   *     equivalent to the empty string, as this method does.   */  @Deprecated public static String list2String(      Collection<?> in, String separator) {    return Collection2String(in, separator);  }  /**   * This concatenates the elements of a collection in a string   *   * @param in - the collection that has to be conatenated   * @param separator - a string to sepparate the elements from the list   *   * @return String   *   * @deprecated Please use   *     But note that {@code Join} does not consider null elements to be   *     equivalent to the empty string, as this method does.   */  @Deprecated public static String Collection2String(      Collection<?> in, String separator) {    if (in == null) {      return null;    }    return Iterator2String(in.iterator(), separator);  }  /**   * @deprecated Please use   *     But note that {@code Join} does not consider null elements to be   *     equivalent to the empty string, as this method does.   */  @Deprecated public static String Iterator2String(      Iterator<?> it, String separator) {    if (it == null) {      return null;    }    StringBuilder out = new StringBuilder();    while (it.hasNext()) {      if (out.length() > 0) {        out.append(separator);      }      out.append(it.next().toString());    }    return out.toString();  }  /**   * This converts a string to a Map. It will first split the string into   * entries using delimEntry. Then each entry is split into a key and a value   * using delimKey. By default we strip the keys. Use doStripEntry to strip   * also the entries   *   * @param in - the string to be processed   * @param delimEntry - delimiter for the entries   * @param delimKey - delimiter between keys and values   * @param doStripEntry - strip entries before inserting in the map   *   * @return HashMap   */  public static HashMap<String, String> string2Map(String in, String delimEntry,      String delimKey, boolean doStripEntry) {    if (in == null) {      return null;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -