加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

常用java字符串操作函数

发布时间:2020-12-15 00:12:54 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 /** * 分割字符串 * * @param str String 原始字符串 * @param splitsign String 分隔符 * @return String[] 分割后的字符串数组 */@SuppressWarnings

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

 
/**
   * 分割字符串
   *
   * @param str String 原始字符串
   * @param splitsign String 分隔符
   * @return String[] 分割后的字符串数组
   */
@SuppressWarnings("unchecked")
public static String[] split(String str,String splitsign) {
    int index;
    if (str == null || splitsign == null)
      return null;
    ArrayList al = new ArrayList();
    while ((index = str.indexOf(splitsign)) != -1) {
      al.add(str.substring(0,index));
      str = str.substring(index + splitsign.length());
    }
    al.add(str);
    return (String[]) al.toArray(new String[0]);
}

/**
   * 替换字符串
   *
   * @param from String 原始字符串
   * @param to String 目标字符串
   * @param source String 母字符串
   * @return String 替换后的字符串
   */
public static String replace(String from,String to,String source) {
    if (source == null || from == null || to == null)
      return null;
    StringBuffer bf = new StringBuffer("");
    int index = -1;
    while ((index = source.indexOf(from)) != -1) {
      bf.append(source.substring(0,index) + to);
      source = source.substring(index + from.length());
      index = source.indexOf(from);
    }
    bf.append(source);
    return bf.toString();
}

/**
   * 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号)
   *
   * @param str String 原始字符串
   * @return String 替换后的字符串
   */
public static String htmlencode(String str) {
    if (str == null) {
      return null;
    }

    return replace(""","&quot;",replace("<","&lt;",str));
}

/**
   * 替换字符串,将被编码的转换成原始码(替换成双引号和小于号)
   *
   * @param str String
   * @return String
   */
public static String htmldecode(String str) {
    if (str == null) {
      return null;
    }

    return replace("&quot;",""",replace("&lt;","<",str));
}

private static final String _BR = "<br/>";

/**
   * 在页面上直接显示文本内容,替换小于号,空格,回车,TAB
   *
   * @param str String 原始字符串
   * @return String 替换后的字符串
   */
public static String htmlshow(String str) {
    if (str == null) {
      return null;
    }

    str = replace("<",str);
    str = replace(" ","&nbsp;",str);
    str = replace("rn",_BR,str);
    str = replace("n",str);
    str = replace("t","&nbsp;&nbsp;&nbsp;&nbsp;",str);
    return str;
}

/**
   * 返回指定字节长度的字符串
   *
   * @param str String 字符串
   * @param length int 指定长度
   * @return String 返回的字符串
   */
public static String toLength(String str,int length) {
    if (str == null) {
      return null;
    }
    if (length <= 0) {
      return "";
    }
    try {
      if (str.getBytes("GBK").length <= length) {
        return str;
      }
    } catch (Exception ex) {
    }
    StringBuffer buff = new StringBuffer();

    int index = 0;
    char c;
    length -= 3;
    while (length > 0) {
      c = str.charAt(index);
      if (c < 128) {
        length--;
      } else {
        length--;
        length--;
      }
      buff.append(c);
      index++;
    }
    buff.append("...");
    return buff.toString();
}

/**
   * 判断是否为整数
   *
   * @param str 传入的字符串
   * @return 是整数返回true,否则返回false
   */
public static boolean isInteger(String str) {
    Pattern pattern = Pattern.compile("^[-\+]?[\d]*$");
    return pattern.matcher(str).matches();
}

/**
   * 判断是否为浮点数,包括double和float
   *
   * @param str 传入的字符串
   * @return 是浮点数返回true,否则返回false
   */
public static boolean isDouble(String str) {
    Pattern pattern = Pattern.compile("^[-\+]?[.\d]*$");
    return pattern.matcher(str).matches();
}

/**
   * 判断输入的字符串是否符合Email样式.
   *
   * @param str 传入的字符串
   * @return 是Email样式返回true,否则返回false
   */
public static boolean isEmail(String str) {
    Pattern pattern = Pattern.compile("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
    return pattern.matcher(str).matches();
}

/**
   * 判断输入的字符串是否为纯汉字
   *
   * @param str 传入的字符窜
   * @return 如果是纯汉字返回true,否则返回false
   */
public static boolean isChinese(String str) {
    Pattern pattern = Pattern.compile("[u0391-uFFE5]+$");
    return pattern.matcher(str).matches();
}

/**
   * 是否为空白,包括null和""
   *
   * @param str
   * @return
   */
public static boolean isBlank(String str) {
    return str == null || str.trim().length() == 0;
}

/**
* 判断是不是合法手机
* handset 手机号码
*/
public static boolean isHandset(String handset) {
try {
   if(!handset.substring(0,1).equals("1")) {
    return false;
   }
   if (handset==null || handset.length()!=11) {
    return false;
   }
   String check = "^[0123456789]+$";
   Pattern regex = Pattern.compile(check);
   Matcher matcher = regex.matcher(handset);
   boolean isMatched = matcher.matches();
   if(isMatched) {
    return true;
   } else {
    return false;
   }
} catch (RuntimeException e) {
   return false;
}
}
}


以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读