JavaUtilString公用类

自己以往项目中收集整理的一些String操作:
package platform.commons.utils;

import java.util.ArrayList;

public class UtilString
{
  public String strSuper;
  protected String lowcaseSuper;

  public UtilString()
  {
    this.strSuper = new String();
    this.lowcaseSuper = this.strSuper;
  }

  public UtilString(String strInit) {
    this.strSuper = new String(strInit);
    this.lowcaseSuper = strInit.toLowerCase();
  }

  public ArrayList<String> split(String strSign) {
    int begin = 0;
    int end = 0;
    ArrayList vecList = new ArrayList();
    if (strSign == "")
    {
      for (int i = 0; i < this.strSuper.length(); i++) {
        vecList.add(this.strSuper.substring(i, i + 1));
      }
      return vecList;
    }
    end = this.strSuper.indexOf(strSign);
    if (end == -1) {
      vecList.add(this.strSuper);
      return vecList;
    }
    do {
      vecList.add(this.strSuper.substring(begin, end));
      begin = end + strSign.length();
      end = this.strSuper.indexOf(strSign, begin);
    }
    while (end >= 0);

    vecList.add(this.strSuper.substring(begin, this.strSuper.length()));
    return vecList;
  }

  public int matchOf(String strBegin)
  {
    return matchOf(strBegin, false);
  }

  public int matchOf(String strBegin, boolean ignoreCase) {
    int pos = -1;

    if (ignoreCase)
      pos = this.lowcaseSuper.indexOf(strBegin.toLowerCase());
    else {
      pos = this.strSuper.indexOf(strBegin);
    }

    if (pos < 0) {
      return pos;
    }
    return pos + strBegin.length();
  }

  public int lastMatchOf(String strEnd) {
    return lastMatchOf(strEnd, false);
  }

  public int lastMatchOf(String strEnd, boolean ignoreCase) {
    if (ignoreCase) {
      return this.lowcaseSuper.lastIndexOf(strEnd.toLowerCase());
    }
    return this.strSuper.lastIndexOf(strEnd);
  }

  public String matchValue(String strBegin, String strEnd)
  {
    return matchValue(strBegin, strEnd, false);
  }

  public String matchValue(String strBegin, String strEnd, boolean ignoreCase) {
    String strResult = new String();
    int posBegin = matchOf(strBegin, ignoreCase);
    int posEnd = lastMatchOf(strEnd, ignoreCase);

    if ((posEnd > posBegin) && (posBegin != -1)) {
      strResult = this.strSuper.substring(posBegin, posEnd);
    }

    return strResult;
  }

  public String replace(String strOld, String strNew) {
    return replace(strOld, strNew, false);
  }

  public String replace(String strOld, String strNew, boolean ignoreCase)
  {
    String strResult = new String();
    String strReplace;
    String strCore;
    String strReplace;
    if (ignoreCase) {
      String strCore = this.lowcaseSuper;
      strReplace = strOld.toLowerCase();
    } else {
      strCore = this.strSuper;
      strReplace = strOld;
    }

    int posBegin = 0;
    int posEnd = strCore.indexOf(strReplace);

    if (posEnd < 0) {
      return this.strSuper;
    }

    do
    {
      strResult = strResult + this.strSuper.substring(posBegin, posEnd);
      strResult = strResult + strNew;
      posBegin = posEnd + strReplace.length();
      posEnd = strCore.indexOf(strReplace, posBegin);
    }
    while (posEnd >= 0);

    strResult = strResult + this.strSuper.substring(posBegin);

    return strResult.toString();
  }

  public String replace(String strBegin, String strEnd, String strNew) {
    return replace(strBegin, strEnd, strNew, false);
  }

  public String replace(String strBegin, String strEnd, String strNew, boolean ignoreCase)
  {
    int posBegin = matchOf(strBegin, ignoreCase);
    int posEnd = lastMatchOf(strEnd, ignoreCase);
    String strResult = new String();

    if ((posEnd >= posBegin) && (posBegin != -1)) {
      strResult = this.strSuper.substring(0, posBegin);
      strResult = strResult + strNew;
      strResult = strResult + this.strSuper.substring(posEnd);

      return strResult;
    }
    return this.strSuper;
  }

  public boolean startsWith(String strBegin)
  {
    return startsWith(strBegin, false);
  }

  public boolean startsWith(String strBegin, boolean ignoreCase)
  {
    if (ignoreCase) {
      return this.lowcaseSuper.startsWith(strBegin.toLowerCase());
    }
    return this.strSuper.startsWith(strBegin);
  }

  public boolean endsWith(String strEnd)
  {
    return endsWith(strEnd, false);
  }

  public boolean endsWith(String strEnd, boolean ignoreCase) {
    if (ignoreCase) {
      return this.lowcaseSuper.endsWith(strEnd.toLowerCase());
    }
    return this.strSuper.endsWith(strEnd);
  }

  public static String cutString(String str, int length)
  {
    if (str == null) {
      return "";
    }

    if ((str.length() > length) && (length > 2)) {
      return str.substring(0, length) + ".";
    }
    return str;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值