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

📄 stringutil.java

📁 google的gdata api包
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* Copyright (c) 2006 Google Inc. * * 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 com.google.gdata.util.common.base;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.StringWriter;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.StringTokenizer;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Some common string manipulation utilities. */public class StringUtil {  public static final String EMPTY_STRING = "";  // \u3000 is the double-byte space character in UTF-8  // \u00A0 is the non-breaking space character (&nbsp;)  // \u2007 is the figure space character (&#8199;)  // \u202F is the narrow non-breaking space character (&#8239;)  public static final String WHITE_SPACES = " \r\n\t\u3000\u00A0\u2007\u202F";  public static final String LINE_BREAKS = "\r\n";  private static final Pattern htmlTagPattern =    Pattern.compile("</?[a-zA-Z][^>]*>");  private static final Pattern characterReferencePattern =    Pattern.compile("&#?[a-zA-Z0-9]{1,8};");  private static final Pattern dbSpecPattern =    Pattern.compile("(.*)\\{(\\d+),(\\d+)\\}(.*)");  // This class should not be instantiated, hence the private constructor  private StringUtil() {}  /** Split "str" by run of delimiters and return. */  public static String[] split(String str, String delims) {    return split(str, delims, false);  }  /**   * Split "str" into tokens by delimiters and optionally remove white spaces   * from the splitted tokens.   *   * @param trimTokens if true, then trim the tokens   */  public static String[] split(String str, String delims, boolean trimTokens) {    StringTokenizer tokenizer = new StringTokenizer(str, delims);    int n = tokenizer.countTokens();    String[] list = new String[n];    for (int i = 0; i < n; i++) {      if (trimTokens) {        list[i] = tokenizer.nextToken().trim();      } else {        list[i] = tokenizer.nextToken();      }    }    return list;  }  /**   * Short hand for <code>split(str, delims, true)</code>   */  public static String[] splitAndTrim(String str, String delims) {    return split(str, delims, true);  }  /** Parse comma-separated list of ints and return as array. */  public static int[] splitInts(String str) throws IllegalArgumentException {    StringTokenizer tokenizer = new StringTokenizer(str, ",");    int n = tokenizer.countTokens();    int[] list = new int[n];    for (int i = 0; i < n; i++) {      String token = tokenizer.nextToken();      list[i] = Integer.parseInt(token);    }    return list;  }  /** Parse comma-separated list of longs and return as array. */  public static long[] splitLongs(String str) throws IllegalArgumentException {    StringTokenizer tokenizer = new StringTokenizer(str, ",");    int n = tokenizer.countTokens();    long[] list = new long[n];    for (int i = 0; i < n; i++) {      String token = tokenizer.nextToken();      list[i] = Long.parseLong(token);    }    return list;  }  /**   * Concatenates the given int[] array into one String, inserting a delimiter   * between each pair of elements.   */  public static String joinInts(int[] tokens, String delimiter) {    if (tokens == null) return "";    StringBuilder result = new StringBuilder();    for (int i = 0; i < tokens.length; i++) {      if (i > 0 && delimiter != null) {        result.append(delimiter);      }      result.append(String.valueOf(tokens[i]));    }    return result.toString();  }  /**   * Concatenates the given long[] array into one String, inserting a delimiter   * between each pair of elements.   */  public static String joinLongs(long[] tokens, String delimiter) {    if (tokens == null) return "";    StringBuilder result = new StringBuilder();    for (int i = 0; i < tokens.length; i++) {      if (i > 0 && delimiter != null) {        result.append(delimiter);      }      result.append(String.valueOf(tokens[i]));    }    return result.toString();  }  /**   * Concatenates the String representations of the elements of a   * String[] array into one String, and inserts a delimiter between   * each pair of elements.   * <p>   * This includes the String[] case, because if s is a String, then   * s.toString() returns s.   *   * @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 join(Object[] tokens, String delimiter) {    if (tokens == null || tokens.length == 0) return "";    StringBuilder result = new StringBuilder();    for (int i = 0; i < tokens.length; i++) {      if (i > 0 && delimiter != null)        result.append(delimiter);      if (tokens[i] != null)        result.append(tokens[i].toString());      }    return result.toString();  }  /**   * Same as {@link #join(Object[],String)}, but takes a {@link Collection}   * instead.   *   * @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 join(Collection tokens, String delimiter) {    return join(tokens.toArray(), delimiter);  }  /** This replaces the occurances of 'what' in 'str' with 'with'   * @param str - the string o process   * @param what - to replace   * @param with - replace with this   * @return String str whete 'what' was repalced with 'with'   *   * @deprecated Please use {@link String#replace(CharSequence, CharSequence)}.   */  @Deprecated  public static String replace(String str, String what, String with) {    // Have to check this argument, for compatibility with the old impl.    // For the record, String.replace() is capable of handling an empty target    // string... but it does something kind of weird in that case.    assert(what.length() > 0);    return str.replace(what, with);  }  /**   * Reformats the given string to a fixed width by inserting   * carriage returns and trimming unnecessary whitespace.   *   * @param str the string to format   * @param width the fixed width (in characters)   */  public static String fixedWidth(String str, int width) {    String[] lines = split(str, "\n");    return fixedWidth(lines, width);  }  /**   * Reformats the given array of lines to a fixed width by inserting   * carriage returns and trimming unnecessary whitespace.   *   * @param lines - array of lines to format   * @param width - the fixed width (in characters)   */  public static String fixedWidth(String[] lines, int width) {    StringBuilder formatStr = new StringBuilder();    for (int i = 0; i < lines.length; i++) {      int curWidth = 0;      if (i != 0) {        formatStr.append("\n");      }      // a small optimization      if (lines[i].length() <= width) {        formatStr.append(lines[i]);        continue;      }      String[] words = splitAndTrim(lines[i], WHITE_SPACES);      for (int j = 0; j < words.length; j++) {        if (curWidth == 0 || (curWidth + words[j].length()) < width) {          // add a space if we're not at the beginning of a line          if (curWidth != 0) {            formatStr.append(" ");            curWidth += 1;          }          curWidth += words[j].length();          formatStr.append(words[j]);        } else {          formatStr.append("\n");          curWidth = words[j].length();          formatStr.append(words[j]);        }      }    }    return formatStr.toString();  }   /**   * Inserts spaces every splitLen characters so that the string will wrap.   *   * @param    lineLen    the length of the substrings to separate with spaces.   * @param    original   the original String   *   * @return   original String with spaces inserted every lineLen characters.   *   */  public static String insertBreakingWhitespace(int lineLen, String original) {    if (original == null || lineLen <= 0) throw new IllegalArgumentException();    int length = original.length();     if (length <= lineLen)     // we can avoid the overhead of instantiating a StringBuilder       return original;    int currPos = 0;    StringBuilder retval = new StringBuilder();    while (length - currPos > lineLen) {      retval.append(original.substring(currPos, currPos + lineLen));      currPos += lineLen;      retval.append(" ");    }    retval.append(original.substring(currPos, length));    return retval.toString();  }  /**   * Indents the given String per line.   * @param iString The string to indent.   * @param iIndentDepth The depth of the indentation.   * @return The indented string.   */  public static String indent(String iString, int iIndentDepth) {    StringBuilder spacer = new StringBuilder();    spacer.append("\n");    for (int i = 0; i < iIndentDepth; i ++) {      spacer.append("  ");    }    return replace(iString, "\n", spacer.toString());  }  /**   * This is a both way strip   *   * @param str the string to strip   * @param left strip from left   * @param right strip from right   * @param what character(s) to strip   * @return the stripped string   */  public static String megastrip(String str,                                 boolean left, boolean right,                                 String what) {    if (str == null) {      return null;    }    int limitLeft = 0;    int limitRight = str.length() - 1;    while (left && limitLeft <= limitRight &&           what.indexOf(str.charAt(limitLeft)) >= 0) {      limitLeft ++;    }    while (right && limitRight>=limitLeft &&           what.indexOf(str.charAt(limitRight)) >= 0) {      limitRight --;    }    return str.substring(limitLeft, limitRight + 1);  }  /** lstrip - strips spaces from left   * @param str what to strip   * @return String the striped string   */  public static String lstrip(String str) {    return megastrip(str, true, false, WHITE_SPACES);  }  /** rstrip - strips spaces from right   * @param str what to strip   * @return String the striped string   */  public static String rstrip(String str) {    return megastrip(str, false, true, WHITE_SPACES);  }  /** strip - strips both ways   * @param str what to strip   * @return String the striped string   */  public static String strip(String str) {    return megastrip(str, true, true, WHITE_SPACES);  }  /** Strip white spaces from both end, and collapse white spaces   * in the middle.   * @param str what to strip   * @return String the striped and collapsed string   */  public static String stripAndCollapse(String str) {    return collapseWhitespace(strip(str));  }  /**   * Give me a string and a potential prefix, and I return the string   * following the prefix if the prefix matches, else null.   * Analogous to the c++ functions strprefix and var_strprefix.   */

⌨️ 快捷键说明

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