首页

关于jsoup源码包中StringUtil字符串工具类进行字符串连接、空格判断、是否为数字等源码说明

标签:jsoup,StringUtil,字符串工具类     发布时间:2018-11-11   

一、前言

关于jsoup(1.6.1)源码包中org.jsoup.helper.StringUtil字符串工具类,对字符串进行连接(join)、间隔过滤(padding)、是否有空格(isBlank)、是否是数字型(isNumeric)等,详情参见源码说明。

二、源码说明

package org.jsoup.helper;@b@@b@import java.util.Collection;@b@import java.util.Iterator;@b@@b@public final class StringUtil@b@{@b@  private static final String[] padding = { "", " ", "  ", "   ", "    ", "     ", "      ", "       ", "        ", "         ", "          " };@b@@b@  public static String join(Collection strings, String sep)@b@  {@b@    return join(strings.iterator(), sep);@b@  }@b@@b@  public static String join(Iterator strings, String sep)@b@  {@b@    if (!(strings.hasNext()))@b@      return "";@b@@b@    String start = strings.next().toString();@b@    if (!(strings.hasNext()))@b@      return start;@b@@b@    StringBuilder sb = new StringBuilder(64).append(start);@b@    while (strings.hasNext()) {@b@      sb.append(sep);@b@      sb.append(strings.next());@b@    }@b@    return sb.toString();@b@  }@b@@b@  public static String padding(int width)@b@  {@b@    if (width < 0)@b@      throw new IllegalArgumentException("width must be > 0");@b@@b@    if (width < padding.length)@b@      return padding[width];@b@@b@    char[] out = new char[width];@b@    for (int i = 0; i < width; ++i)@b@      out[i] = ' ';@b@    return String.valueOf(out);@b@  }@b@@b@  public static boolean isBlank(String string)@b@  {@b@    if ((string == null) || (string.length() == 0))@b@      return true;@b@@b@    int l = string.length();@b@    for (int i = 0; i < l; ++i)@b@      if (!(Character.isWhitespace(string.codePointAt(i))))@b@        return false;@b@@b@    return true;@b@  }@b@@b@  public static boolean isNumeric(String string)@b@  {@b@    if ((string == null) || (string.length() == 0))@b@      return false;@b@@b@    int l = string.length();@b@    for (int i = 0; i < l; ++i)@b@      if (!(Character.isDigit(string.codePointAt(i))))@b@        return false;@b@@b@    return true;@b@  }@b@@b@  public static String normaliseWhitespace(String string) {@b@    StringBuilder sb = new StringBuilder(string.length());@b@@b@    boolean lastWasWhite = false;@b@    boolean modified = false;@b@@b@    int l = string.length();@b@    for (int i = 0; i < l; ++i) {@b@      int c = string.codePointAt(i);@b@      if (Character.isWhitespace(c)) {@b@        if (lastWasWhite) {@b@          modified = true;@b@        }@b@        else {@b@          if (c != 32)@b@            modified = true;@b@          sb.append(' ');@b@          lastWasWhite = true;@b@        }@b@      } else {@b@        sb.appendCodePoint(c);@b@        lastWasWhite = false;@b@      }@b@    }@b@    return ((modified) ? sb.toString() : string);@b@  }@b@@b@  public static boolean in(String needle, String[] haystack) {@b@    String[] arr$ = haystack; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String hay = arr$[i$];@b@      if (hay.equals(needle))@b@        return true;@b@    }@b@    return false;@b@  }@b@}