欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java自定义封装StringUtils常用工具类

程序员文章站 2022-06-11 11:51:08
自定义封装stringutils常用工具类,供大家参考,具体内容如下 package com.demo.utils; import java.util....

自定义封装stringutils常用工具类,供大家参考,具体内容如下

package com.demo.utils;

import java.util.arraylist;
import java.util.list;
import java.util.map;

/**
 * 字符串操作工具类
 * @author dongyangyang
 * @date 2016/12/28 23:12
 * @version 1.0
 *
 */
public class stringutils {

 /**
  * 首字母变小写
  * @param str
  * @return
  */
 public static string firstchartolowercase(string str) {
  char firstchar = str.charat(0);
  if (firstchar >= 'a' && firstchar <= 'z') {
   char[] arr = str.tochararray();
   arr[0] += ('a' - 'a');
   return new string(arr);
  }
  return str;
 }

 /**
  * 首字母变大写
  * @param str
  * @return
  */
 public static string firstchartouppercase(string str) {
  char firstchar = str.charat(0);
  if (firstchar >= 'a' && firstchar <= 'z') {
   char[] arr = str.tochararray();
   arr[0] -= ('a' - 'a');
   return new string(arr);
  }
  return str;
 }

 /**
  * 判断是否为空
  * @param str
  * @return
  */
 public static boolean isempty(final string str) {
  return (str == null) || (str.length() == 0);
 }

 /**
  * 判断是否不为空
  * @param str
  * @return
  */
 public static boolean isnotempty(final string str) {
  return !isempty(str);
 }

 /**
  * 判断是否空白
  * @param str
  * @return
  */
 public static boolean isblank(final string str) {
  int strlen;
  if ((str == null) || ((strlen = str.length()) == 0))
   return true;
  for (int i = 0; i < strlen; i++) {
   if (!character.iswhitespace(str.charat(i))) {
    return false;
   }
  }
  return true;
 }

 /**
  * 判断是否不是空白
  * @param str
  * @return
  */
 public static boolean isnotblank(final string str) {
  return !isblank(str);
 }

 /**
  * 判断多个字符串全部是否为空
  * @param strings
  * @return
  */
 public static boolean isallempty(string... strings) {
  if (strings == null) {
   return true;
  }
  for (string str : strings) {
   if (isnotempty(str)) {
    return false;
   }
  }
  return true;
 }

 /**
  * 判断多个字符串其中任意一个是否为空
  * @param strings
  * @return
  */
 public static boolean ishasempty(string... strings) {
  if (strings == null) {
   return true;
  }
  for (string str : strings) {
   if (isempty(str)) {
    return true;
   }
  }
  return false;
 }

 /**
  * checkvalue为 null 或者为 "" 时返回 defaultvalue
  * @param checkvalue
  * @param defaultvalue
  * @return
  */
 public static string isempty(string checkvalue, string defaultvalue) {
  return isempty(checkvalue) ? defaultvalue : checkvalue;
 }

 /**
  * 字符串不为 null 而且不为 "" 并且等于other
  * @param str
  * @param other
  * @return
  */
 public static boolean isnotemptyandequelsother(string str, string other) {
  if (isempty(str)) {
   return false;
  }
  return str.equals(other);
 }

 /**
  * 字符串不为 null 而且不为 "" 并且不等于other
  * @param str
  * @param other
  * @return
  */
 public static boolean isnotemptyandnotequelsother(string str, string... other) {
  if (isempty(str)) {
   return false;
  }
  for (int i = 0; i < other.length; i++) {
   if (str.equals(other[i])) {
    return false;
   }
  }
  return true;
 }

 /**
  * 字符串不等于other
  * @param str
  * @param other
  * @return
  */
 public static boolean isnotequelsother(string str, string... other) {
  for (int i = 0; i < other.length; i++) {
   if (other[i].equals(str)) {
    return false;
   }
  }
  return true;
 }

 /**
  * 判断字符串不为空
  * @param strings
  * @return
  */
 public static boolean isnotempty(string... strings) {
  if (strings == null) {
   return false;
  }
  for (string str : strings) {
   if (str == null || "".equals(str.trim())) {
    return false;
   }
  }
  return true;
 }

 /**
  * 比较字符相等
  * @param value
  * @param equals
  * @return
  */
 public static boolean equals(string value, string equals) {
  if (isallempty(value, equals)) {
   return true;
  }
  return value.equals(equals);
 }

 /**
  * 比较字符串不相等
  * @param value
  * @param equals
  * @return
  */
 public static boolean isnotequals(string value, string equals) {
  return !equals(value, equals);
 }

 public static string[] split(string content, string separatorchars) {
  return splitworker(content, separatorchars, -1, false);
 }

 public static string[] split(string str, string separatorchars, int max) {
  return splitworker(str, separatorchars, max, false);
 }

 public static final string[] empty_string_array = new string[0];

 private static string[] splitworker(string str, string separatorchars, int max, boolean preservealltokens) {
  if (str == null) {
   return null;
  }
  int len = str.length();
  if (len == 0) {
   return empty_string_array;
  }
  list<string> list = new arraylist<string>();
  int sizeplus1 = 1;
  int i = 0, start = 0;
  boolean match = false;
  boolean lastmatch = false;
  if (separatorchars == null) {
   while (i < len) {
    if (character.iswhitespace(str.charat(i))) {
     if (match || preservealltokens) {
      lastmatch = true;
      if (sizeplus1++ == max) {
       i = len;
       lastmatch = false;
      }
      list.add(str.substring(start, i));
      match = false;
     }
     start = ++i;
     continue;
    }
    lastmatch = false;
    match = true;
    i++;
   }
  } else if (separatorchars.length() == 1) {
   char sep = separatorchars.charat(0);
   while (i < len) {
    if (str.charat(i) == sep) {
     if (match || preservealltokens) {
      lastmatch = true;
      if (sizeplus1++ == max) {
       i = len;
       lastmatch = false;
      }
      list.add(str.substring(start, i));
      match = false;
     }
     start = ++i;
     continue;
    }
    lastmatch = false;
    match = true;
    i++;
   }
  } else {
   while (i < len) {
    if (separatorchars.indexof(str.charat(i)) >= 0) {
     if (match || preservealltokens) {
      lastmatch = true;
      if (sizeplus1++ == max) {
       i = len;
       lastmatch = false;
      }
      list.add(str.substring(start, i));
      match = false;
     }
     start = ++i;
     continue;
    }
    lastmatch = false;
    match = true;
    i++;
   }
  }
  if (match || (preservealltokens && lastmatch)) {
   list.add(str.substring(start, i));
  }
  return (string[]) list.toarray(empty_string_array);
 }

 /**
  * 消除转义字符
  * @param str
  * @return
  */
 public static string escapexml(string str) {
  if (str == null)
   return "";
  stringbuilder sb = new stringbuilder();
  for (int i = 0; i < str.length(); ++i) {
   char c = str.charat(i);
   switch (c) {
   case '\u00ff':
   case '\u0024':
    break;
   case '&':
    sb.append("&");
    break;
   case '<':
    sb.append("<");
    break;
   case '>':
    sb.append(">");
    break;
   case '\"':
    sb.append(""");
    break;
   case '\'':
    sb.append("'");
    break;
   default:
    if (c >= '\u0000' && c <= '\u001f')
     break;
    if (c >= '\ue000' && c <= '\uf8ff')
     break;
    if (c >= '\ufff0' && c <= '\uffff')
     break;
    sb.append(c);
    break;
   }
  }
  return sb.tostring();
 }

 /**
  * 将字符串中特定模式的字符转换成map中对应的值
  *
  * @param s
  *   需要转换的字符串
  * @param map
  *   转换所需的键值对集合
  * @return 转换后的字符串
  */
 public static string replace(string s, map<string, object> map) {
  stringbuilder ret = new stringbuilder((int) (s.length() * 1.5));
  int cursor = 0;
  for (int start, end; (start = s.indexof("${", cursor)) != -1 && (end = s.indexof("}", start)) != -1;) {
   ret.append(s.substring(cursor, start)).append(map.get(s.substring(start + 2, end)));
   cursor = end + 1;
  }
  ret.append(s.substring(cursor, s.length()));
  return ret.tostring();
 }

 public static string replace(string s, object... objs) {
  if (objs == null || objs.length == 0)
   return s;
  if (s.indexof("{}") == -1)
   return s;
  stringbuilder ret = new stringbuilder((int) (s.length() * 1.5));
  int cursor = 0;
  int index = 0;
  for (int start; (start = s.indexof("{}", cursor)) != -1;) {
   ret.append(s.substring(cursor, start));
   if (index < objs.length)
    ret.append(objs[index]);
   else
    ret.append("{}");
   cursor = start + 2;
   index++;
  }
  ret.append(s.substring(cursor, s.length()));
  return ret.tostring();
 }

 /**
  * 字符串格式化工具,参数必须以{0}之类的样式标示出来.大括号中的数字从0开始。
  * 
  * @param source
  *   源字符串
  * @param params
  *   需要替换的参数列表,写入时会调用每个参数的tostring().
  * @return 替换完成的字符串。如果原始字符串为空或者参数为空那么将直接返回原始字符串。
  */
 public static string replaceargs(string source, object... params) {
  if (params == null || params.length == 0 || source == null || source.isempty()) {
   return source;
  }
  stringbuilder buff = new stringbuilder(source);
  stringbuilder temp = new stringbuilder();
  int startindex = 0;
  int endindex = 0;
  string param = null;
  for (int count = 0; count < params.length; count++) {
   if (params[count] == null) {
    param = null;
   } else {
    param = params[count].tostring();
   }

   temp.delete(0, temp.length());
   temp.append("{");
   temp.append(count);
   temp.append("}");
   while (true) {
    startindex = buff.indexof(temp.tostring(), endindex);
    if (startindex == -1) {
     break;
    }
    endindex = startindex + temp.length();

    buff.replace(startindex, endindex, param == null ? "" : param);
   }
   startindex = 0;
   endindex = 0;
  }
  return buff.tostring();
 }

 public static string substringbefore(final string s, final string separator) {
  if (isempty(s) || separator == null) {
   return s;
  }
  if (separator.isempty()) {
   return "";
  }
  final int pos = s.indexof(separator);
  if (pos < 0) {
   return s;
  }
  return s.substring(0, pos);
 }

 public static string substringbetween(final string str, final string open, final string close) {
  if (str == null || open == null || close == null) {
   return null;
  }
  final int start = str.indexof(open);
  if (start != -1) {
   final int end = str.indexof(close, start + open.length());
   if (end != -1) {
    return str.substring(start + open.length(), end);
   }
  }
  return null;
 }

 public static string substringafter(final string str, final string separator) {
  if (isempty(str)) {
   return str;
  }
  if (separator == null) {
   return "";
  }
  final int pos = str.indexof(separator);
  if (pos == -1) {
   return "";
  }
  return str.substring(pos + separator.length());
 }

 /**
 * 转换为字节数组
 * @param str
 * @return 
 */
 public static string tostring(byte[] bytes){
  try {
   return new string(bytes, "utf-8");
  } catch (unsupportedencodingexception e) {
   return null;
  }
 }

 /**
 * 转换为字节数组
 * @param str
 * @return 
 */
 public static byte[] getbytes(string str){
  if (str != null){
  try {
   return str.getbytes("utf-8");
   } catch (unsupportedencodingexception e) {
   return null;
   }
  }else{
  return null;
  }
 }
 public static void main(string[] a){
  string escapexml = escapexml("\\");
  system.out.println(escapexml);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。