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

📄 stringutility.java

📁 网上购物系统
💻 JAVA
字号:
// =========================================================
// 项目:宠物商店

// 路径:$Archive: $
// =========================================================
package example.common.util;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
/**
 * @author zhuds
 * @version $Revision: $ $Date: $
 * 处理字符串共通

 */
public class StringUtility {
  
  /**
   * 把一个字符转化成二进制的
   * 
   * @param c
   *            char, 原字符

   * @return 二进制字符串
   */
  public static String toBin(char c) {
    int k = 0x8000;
    StringBuffer sb = new StringBuffer(16);
    for (int i = 0; i < 16; k >>>= 1, i++) {
      sb.append(((c & k) != 0) ? 1 : 0);
    }
    return sb.toString();
  }

  /**
   * 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符)

   * 
   * @param c
   *            char, 需要判断的字符
   * @return boolean, 返回true,Ascill字符
   */
  public static boolean isLetter(char c) {
    int k = 0x80;
    return c / k == 0 ? true : false;
  }

  
  /**
   * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
   * 
   * @param s
   *            String ,需要得到长度的字符串

   * @return int, 得到的字符串长度
   */
  public static int length(String s) {
    char[] c = s.toCharArray();
    int len = 0;
    for (int i = 0; i < c.length; i++) {
      len++;
      if (!isLetter(c[i])) {
        len++;
      }
    }
    return len;
  }

  /**
   * 从开始位置获取len个字符位的字符串,不区分中英文
   * 
   * @param str
   *            String, 原始字符串

   * @param len
   *            int, 切割的长度(一个汉字长度按2算的)
   * @return String, 切割后的字符串

   */
  public static String substring(String str, int len) {
    return substring(str, 0, len);
  }

  /**
   * 截取一段字符的长度,不区分中英文,如果数字不正好,则多取一个字符位
   * 
   * @param str
   *            String, 原始字符串

   * @param offset
   *            int, 开始位置

   * @param len
   *            int, 截取长度(一个汉字长度按2算的)
   * @return String, 返回的字符串
   */
  public static String substring(String str, int offset, int len) {
    if (str == null) {
      return str;
    }
    int sBegin = (offset < 0) ? 0 : offset;
    // 越出范围处理
    if (len < 1 || sBegin > str.length()) {
      return "";
    }

    try {
      if (len + sBegin > str.getBytes("GBK").length) {
        return str.substring(sBegin);
      }
    } catch (UnsupportedEncodingException e) {
      return "";
    }

    char[] c = str.toCharArray();
    StringBuffer sb = new StringBuffer(c.length);
    int i = sBegin;
    int j = sBegin;
    int chrLen = c.length;
    while (j < chrLen) {
      i++;
      if (!isLetter(c[j])) {
        i++;
      }
      if (i <= sBegin + len * 2) {
        sb.append(c[j]);
      } else {
        break;
      }
      j++;
    }
    return sb.toString();
  }


  /**
   * 缩略给定的字符串。

   * 
   * <pre>
   *       abbreviate(null, *)      = null
   *       abbreviate(&quot;&quot;, 4)        = &quot;&quot;
   *       abbreviate(&quot;a中华gh&quot;, 5) = &quot;a...&quot;
   *       abbreviate(&quot;a中华gh&quot;, 6) = &quot;a中...&quot;
   *       abbreviate(&quot;a中华gh&quot;, 7) = &quot;a中...&quot;
   *       abbreviate(&quot;a中华gh&quot;, 8) = &quot;a中华gh&quot;
   *       abbreviate(&quot;a中华gh&quot;, 9) = &quot;a中华gh&quot;
   *       abbreviate(&quot;abcdefg&quot;, 3) = IllegalArgumentException
   * </pre>
   * 
   * @param str
   *            String, 原始字符串

   * @param maxWidth
   *            int, 截取长度(一个汉字长度按2算的)
   * @return String, 返回的字符串
   * UnsupportedEncodingException  不支持编码异常。

   */
  public static String abbreviate(String str, int maxWidth) {
    return abbreviate(str, 0, maxWidth);
  }
  /**
   * 
   * @param str 原始字符串

   * @param iOffset
   * @param maxWidth 最大的宽度
   * @return 缩略后的字符串

   */
  public static String abbreviate(String str, int iOffset, int maxWidth) {
    int offset = iOffset;
    if (str == null) {
      return null;
    }
    if (maxWidth < 4) {
      throw new IllegalArgumentException(
          "Minimum abbreviation width is 4");
    }
    int strLen = 0;
    try {
      strLen = str.getBytes("GBK").length;
    } catch (UnsupportedEncodingException e) {
      return StringUtils.abbreviate(str, maxWidth - 3);
    }
    if (str.length() <= maxWidth || strLen <= maxWidth * 2) {
      return str;
    }
    if (offset > str.length()) {
      offset = str.length();
    }
    if ((strLen - offset) < (maxWidth - 3)) {
      offset = str.length() - (maxWidth - 3);
    }
    if (offset <= 4) {
      return substring(str, 0, maxWidth - 3) + "...";
    }
    if (maxWidth < 7) {
      throw new IllegalArgumentException(
          "Minimum abbreviation width with offset is 7");
    }
    if ((offset + (maxWidth - 3)) < str.length()) {
      return "..." + abbreviate(str.substring(offset), maxWidth - 3);
    }
    return "..." + substring(str, str.length() - (maxWidth - 3));
  }


  /**
   * 格式化日期YYYY-MM-DD。 创建日期:

   * 
   * @param str
   *          字符串时间

   * @return 格式化之后的字符串

   */
  public String formatData(String str) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date sDate = sdf.parse(str);
      return sdf.format(sDate);
    } catch (Exception e) {
      return str;
    }
  }

}

⌨️ 快捷键说明

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