Java 工具类

1,Spring 属性拷贝BeanUtils

package gx.springboot.schedule.common.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class CopyUtil {

    public static <T> T copy(Object source, Class<T> c) {
        if (source == null) {
            return null;
        }
        try {
            T instance = c.newInstance();
            BeanUtils.copyProperties(source, instance);
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <E, T> List<T> copyList(List<E> sources, Class<T> c) {
        if (CollectionUtils.isEmpty(sources)) {
            return new ArrayList<T>();
        }
        List<T> list = new ArrayList<T>();
        for (E source : sources) {
            list.add(copy(source, c));
        }
        return list;
    }

}

 

2,BigDecimal 保留小数位

 

package com.gx.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public abstract class ChiticDigitUtil {

    public static BigDecimal toScale(String data, int scale) {
        if (StringUtils.isBlank(data)) {
            data = "0";
        }
        BigDecimal bigDecimal = new BigDecimal(data);
        return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal toScale(BigDecimal data, int scale) {
        if (null == data) {
            data = BigDecimal.ZERO;
        }
        BigDecimal bigDecimal = data;
        return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal toScale(Double data, int scale) {
        if (null == data) {
            data = 0D;
        }
        BigDecimal bigDecimal = BigDecimal.valueOf(data);
        return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal toScale(Float data, int scale) {
        if (null == data) {
            data = 0F;
        }
        BigDecimal bigDecimal = BigDecimal.valueOf(data);
        return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

}

除法
BigDecimal bigDecimal = BigDecimal.valueOf(a).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(b), 2, BigDecimal.ROUND_HALF_UP);

 

 

3,Spring应用上下文环境

 

package com.gx.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

@SuppressWarnings("unchecked")
@Component
public class SpringUtils implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        //首字母默认小写
        name=lowerCaseInit(name);
        if (containsBean(name)) {
            return (T) beanFactory.getBean(name);
        }else{
            return null;
        }
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        @SuppressWarnings("unchecked")
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return beanFactory.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
     * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws NoSuchBeanDefinitionException
     *
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return beanFactory.getAliases(name);
    }

    /**
     *首字母小写
     * @return:  小写的首字母
     */

    private static String lowerCaseInit(String str) {
        if (str.length()>0) {
            char c = str.charAt(0);
            if (c >= 65 && c <= 90) {
                int i = c + 32;
                return ((char)i)+str.substring(1);
            }else{
                return str;
            }
        }else{
            return  null;
        }
    }
}

 

 

4,生成UUID twitter的snowflake算法 -- java实现

 

package bdc.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * twitter的snowflake算法 -- java实现
 */
public class SnowFlake {

  /**
   * 起始的时间戳
   * 
   * 某个时间点相对1970-01-01的毫秒数
   */
  private final static long START_STMP = 1501516800000L;

  /**
   * 每一部分占用的位数
   */
  private final static long SEQUENCE_BIT = 12; // 序列号占用的位数
  private final static long MACHINE_BIT = 5; // 机器标识占用的位数
  private final static long DATACENTER_BIT = 5;// 数据中心占用的位数

  /**
   * 每一部分的最大值
   * 
   * -1L^(-1L<<n)表示n个bit的数字最大值 相关知识 异或 位移 原码 反码 补码
   */
  private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
  private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
  private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);

  /**
   * 每一部分向左的位移
   */
  private final static long MACHINE_LEFT = SEQUENCE_BIT;
  private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
  private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;

  private long datacenterId; // 数据中心
  private long machineId; // 机器标识
  private long sequence = 0L; // 序列号
  private long lastStmp = -1L;// 上一次时间戳

  public SnowFlake(long datacenterId, long machineId) {
    if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
      throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
    }
    if (machineId > MAX_MACHINE_NUM || machineId < 0) {
      throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
    }
    this.datacenterId = datacenterId;
    this.machineId = machineId;
  }

  /**
   * 产生下一个ID
   *
   * @return
   */
  public synchronized long nextId() {
    long currStmp = getNewstmp();
    if (currStmp < lastStmp) {
      throw new RuntimeException("Clock moved backwards.  Refusing to generate id");
    }

    if (currStmp == lastStmp) {
      // 相同毫秒内,序列号自增
      sequence = (sequence + 1) & MAX_SEQUENCE;
      // 同一毫秒的序列数已经达到最大
      if (sequence == 0L) {
        currStmp = getNextMill();
      }
    } else {
      // 不同毫秒内,序列号置为0
      sequence = 0L;
    }

    lastStmp = currStmp;

    return (currStmp - START_STMP) << TIMESTMP_LEFT // 时间戳部分
        | datacenterId << DATACENTER_LEFT // 数据中心部分
        | machineId << MACHINE_LEFT // 机器标识部分
        | sequence; // 序列号部分
  }

  private long getNextMill() {
    long mill = getNewstmp();
    while (mill <= lastStmp) {
      mill = getNewstmp();
    }
    return mill;
  }

  private long getNewstmp() {
    return System.currentTimeMillis();
  }

  public static String getDate(long id, String pattern){
    id = (id >> 22) + START_STMP;
    DateFormat format = new SimpleDateFormat(pattern);
    return format.format(new Date(id));
  }

  public static void main(String[] args) {
    SnowFlake snowFlake = new SnowFlake(2, 3);

    long id = snowFlake.nextId();
    long time = (id >> 22) + START_STMP;
    System.out.println(id);
    Date date = new Date(time);
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    System.out.println(format.format(new Date(START_STMP)));
    System.out.println(format.format(date));
    ;
  }
}

 

 

5,MD5加密

package com.gx.util;

//import required classes

public class MD5 {
    private static MD5 md5 = null;

    static final int S11 = 7;

    static final int S12 = 12;

    static final int S13 = 17;

    static final int S14 = 22;

    static final int S21 = 5;

    static final int S22 = 9;

    static final int S23 = 14;

    static final int S24 = 20;

    static final int S31 = 4;

    static final int S32 = 11;

    static final int S33 = 16;

    static final int S34 = 23;

    static final int S41 = 6;

    static final int S42 = 10;

    static final int S43 = 15;

    static final int S44 = 21;

    static final byte PADDING[] = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0 };

    private long state[];

    private long count[];

    private byte buffer[];

    public String digestHexStr;

    private byte digest[];

    public static synchronized MD5 getInstance() {
        if (md5 == null) {
            md5 = new MD5();
        }
        return md5;
    }

    public String getMD5ofStr(String s) {
        md5Init();
        md5Update(s.getBytes(), s.length());
        md5Final();
        digestHexStr = "";
        for (int i = 0; i < 16; i++) {
            digestHexStr += byteHEX(digest[i]);
        }

        return digestHexStr;
    }

    private MD5() {
        state = new long[4];
        count = new long[2];
        buffer = new byte[64];
        digest = new byte[16];
        md5Init();
    }

    private void md5Init() {
        count[0] = 0L;
        count[1] = 0L;
        state[0] = 0x67452301L;
        state[1] = 0xefcdab89L;
        state[2] = 0x98badcfeL;
        state[3] = 0x10325476L;
    }

    private long F(long l, long l1, long l2) {
        return l & l1 | ~l & l2;
    }

    private long G(long l, long l1, long l2) {
        return l & l2 | l1 & ~l2;
    }

    private long H(long l, long l1, long l2) {
        return l ^ l1 ^ l2;
    }

    private long I(long l, long l1, long l2) {
        return l1 ^ (l | ~l2);
    }

    private long FF(long l, long l1, long l2, long l3, long l4, long l5, long l6) {
        l += F(l1, l2, l3) + l4 + l6;
        l = (int) l << (int) l5 | (int) l >>> (int) (32L - l5);
        l += l1;
        return l;
    }

    private long GG(long l, long l1, long l2, long l3, long l4, long l5, long l6) {
        l += G(l1, l2, l3) + l4 + l6;
        l = (int) l << (int) l5 | (int) l >>> (int) (32L - l5);
        l += l1;
        return l;
    }

    private long HH(long l, long l1, long l2, long l3, long l4, long l5, long l6) {
        l += H(l1, l2, l3) + l4 + l6;
        l = (int) l << (int) l5 | (int) l >>> (int) (32L - l5);
        l += l1;
        return l;
    }

    private long II(long l, long l1, long l2, long l3, long l4, long l5, long l6) {
        l += I(l1, l2, l3) + l4 + l6;
        l = (int) l << (int) l5 | (int) l >>> (int) (32L - l5);
        l += l1;
        return l;
    }

    private void md5Update(byte abyte0[], int i) {
        byte abyte1[] = new byte[64];
        int k = (int) (count[0] >>> 3) & 0x3f;
        if ((count[0] += i << 3) < (long) (i << 3)) {
            count[1]++;
        }
        count[1] += i >>> 29;
        int l = 64 - k;
        int j;
        if (i >= l) {
            md5Memcpy(buffer, abyte0, k, 0, l);
            md5Transform(buffer);
            for (j = l; j + 63 < i; j += 64) {
                md5Memcpy(abyte1, abyte0, 0, j, 64);
                md5Transform(abyte1);
            }

            k = 0;
        } else {
            j = 0;
        }
        md5Memcpy(buffer, abyte0, k, j, i - j);
    }

    private void md5Final() {
        byte abyte0[] = new byte[8];
        Encode(abyte0, count, 8);
        int i = (int) (count[0] >>> 3) & 0x3f;
        int j = i >= 56 ? 120 - i : 56 - i;
        md5Update(PADDING, j);
        md5Update(abyte0, 8);
        Encode(digest, state, 16);
    }

    private void md5Memcpy(byte abyte0[], byte abyte1[], int i, int j, int k) {
        for (int l = 0; l < k; l++) {
            abyte0[i + l] = abyte1[j + l];
        }

    }

    private void md5Transform(byte abyte0[]) {
        long l = state[0];
        long l1 = state[1];
        long l2 = state[2];
        long l3 = state[3];
        long al[] = new long[16];
        Decode(al, abyte0, 64);
        l = FF(l, l1, l2, l3, al[0], 7L, 0xd76aa478L);
        l3 = FF(l3, l, l1, l2, al[1], 12L, 0xe8c7b756L);
        l2 = FF(l2, l3, l, l1, al[2], 17L, 0x242070dbL);
        l1 = FF(l1, l2, l3, l, al[3], 22L, 0xc1bdceeeL);
        l = FF(l, l1, l2, l3, al[4], 7L, 0xf57c0fafL);
        l3 = FF(l3, l, l1, l2, al[5], 12L, 0x4787c62aL);
        l2 = FF(l2, l3, l, l1, al[6], 17L, 0xa8304613L);
        l1 = FF(l1, l2, l3, l, al[7], 22L, 0xfd469501L);
        l = FF(l, l1, l2, l3, al[8], 7L, 0x698098d8L);
        l3 = FF(l3, l, l1, l2, al[9], 12L, 0x8b44f7afL);
        l2 = FF(l2, l3, l, l1, al[10], 17L, 0xffff5bb1L);
        l1 = FF(l1, l2, l3, l, al[11], 22L, 0x895cd7beL);
        l = FF(l, l1, l2, l3, al[12], 7L, 0x6b901122L);
        l3 = FF(l3, l, l1, l2, al[13], 12L, 0xfd987193L);
        l2 = FF(l2, l3, l, l1, al[14], 17L, 0xa679438eL);
        l1 = FF(l1, l2, l3, l, al[15], 22L, 0x49b40821L);
        l = GG(l, l1, l2, l3, al[1], 5L, 0xf61e2562L);
        l3 = GG(l3, l, l1, l2, al[6], 9L, 0xc040b340L);
        l2 = GG(l2, l3, l, l1, al[11], 14L, 0x265e5a51L);
        l1 = GG(l1, l2, l3, l, al[0], 20L, 0xe9b6c7aaL);
        l = GG(l, l1, l2, l3, al[5], 5L, 0xd62f105dL);
        l3 = GG(l3, l, l1, l2, al[10], 9L, 0x2441453L);
        l2 = GG(l2, l3, l, l1, al[15], 14L, 0xd8a1e681L);
        l1 = GG(l1, l2, l3, l, al[4], 20L, 0xe7d3fbc8L);
        l = GG(l, l1, l2, l3, al[9], 5L, 0x21e1cde6L);
        l3 = GG(l3, l, l1, l2, al[14], 9L, 0xc33707d6L);
        l2 = GG(l2, l3, l, l1, al[3], 14L, 0xf4d50d87L);
        l1 = GG(l1, l2, l3, l, al[8], 20L, 0x455a14edL);
        l = GG(l, l1, l2, l3, al[13], 5L, 0xa9e3e905L);
        l3 = GG(l3, l, l1, l2, al[2], 9L, 0xfcefa3f8L);
        l2 = GG(l2, l3, l, l1, al[7], 14L, 0x676f02d9L);
        l1 = GG(l1, l2, l3, l, al[12], 20L, 0x8d2a4c8aL);
        l = HH(l, l1, l2, l3, al[5], 4L, 0xfffa3942L);
        l3 = HH(l3, l, l1, l2, al[8], 11L, 0x8771f681L);
        l2 = HH(l2, l3, l, l1, al[11], 16L, 0x6d9d6122L);
        l1 = HH(l1, l2, l3, l, al[14], 23L, 0xfde5380cL);
        l = HH(l, l1, l2, l3, al[1], 4L, 0xa4beea44L);
        l3 = HH(l3, l, l1, l2, al[4], 11L, 0x4bdecfa9L);
        l2 = HH(l2, l3, l, l1, al[7], 16L, 0xf6bb4b60L);
        l1 = HH(l1, l2, l3, l, al[10], 23L, 0xbebfbc70L);
        l = HH(l, l1, l2, l3, al[13], 4L, 0x289b7ec6L);
        l3 = HH(l3, l, l1, l2, al[0], 11L, 0xeaa127faL);
        l2 = HH(l2, l3, l, l1, al[3], 16L, 0xd4ef3085L);
        l1 = HH(l1, l2, l3, l, al[6], 23L, 0x4881d05L);
        l = HH(l, l1, l2, l3, al[9], 4L, 0xd9d4d039L);
        l3 = HH(l3, l, l1, l2, al[12], 11L, 0xe6db99e5L);
        l2 = HH(l2, l3, l, l1, al[15], 16L, 0x1fa27cf8L);
        l1 = HH(l1, l2, l3, l, al[2], 23L, 0xc4ac5665L);
        l = II(l, l1, l2, l3, al[0], 6L, 0xf4292244L);
        l3 = II(l3, l, l1, l2, al[7], 10L, 0x432aff97L);
        l2 = II(l2, l3, l, l1, al[14], 15L, 0xab9423a7L);
        l1 = II(l1, l2, l3, l, al[5], 21L, 0xfc93a039L);
        l = II(l, l1, l2, l3, al[12], 6L, 0x655b59c3L);
        l3 = II(l3, l, l1, l2, al[3], 10L, 0x8f0ccc92L);
        l2 = II(l2, l3, l, l1, al[10], 15L, 0xffeff47dL);
        l1 = II(l1, l2, l3, l, al[1], 21L, 0x85845dd1L);
        l = II(l, l1, l2, l3, al[8], 6L, 0x6fa87e4fL);
        l3 = II(l3, l, l1, l2, al[15], 10L, 0xfe2ce6e0L);
        l2 = II(l2, l3, l, l1, al[6], 15L, 0xa3014314L);
        l1 = II(l1, l2, l3, l, al[13], 21L, 0x4e0811a1L);
        l = II(l, l1, l2, l3, al[4], 6L, 0xf7537e82L);
        l3 = II(l3, l, l1, l2, al[11], 10L, 0xbd3af235L);
        l2 = II(l2, l3, l, l1, al[2], 15L, 0x2ad7d2bbL);
        l1 = II(l1, l2, l3, l, al[9], 21L, 0xeb86d391L);
        state[0] += l;
        state[1] += l1;
        state[2] += l2;
        state[3] += l3;
    }

    private void Encode(byte abyte0[], long al[], int i) {
        int j = 0;
        for (int k = 0; k < i; k += 4) {
            abyte0[k] = (byte) (int) (al[j] & 255L);
            abyte0[k + 1] = (byte) (int) (al[j] >>> 8 & 255L);
            abyte0[k + 2] = (byte) (int) (al[j] >>> 16 & 255L);
            abyte0[k + 3] = (byte) (int) (al[j] >>> 24 & 255L);
            j++;
        }

    }

    private void Decode(long al[], byte abyte0[], int i) {
        int j = 0;
        for (int k = 0; k < i; k += 4) {
            al[j] = b2iu(abyte0[k]) | b2iu(abyte0[k + 1]) << 8
                    | b2iu(abyte0[k + 2]) << 16 | b2iu(abyte0[k + 3]) << 24;
            j++;
        }

    }

    public static long b2iu(byte byte0) {
        return byte0 >= 0 ? byte0 : byte0 & 0xff;
    }

    public static String byteHEX(byte byte0) {
        char ac[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                'B', 'C', 'D', 'E', 'F' };
        char ac1[] = new char[2];
        ac1[0] = ac[byte0 >>> 4 & 0xf];
        ac1[1] = ac[byte0 & 0xf];
        String s = new String(ac1);
        return s;
    }

    public static String getMD5Str(String string) {
        return getInstance().getMD5ofStr(string);
    }

}

使用:

MD5.getMD5Str("admin")

 

6,StringUtils工具类

1. public static boolean isEmpty(String str)
   判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
   下面是 StringUtils 判断是否为空的示例:

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty("   ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false



2. public static boolean isNotEmpty(String str)
   判断某字符串是否非空,等于 !isEmpty(String str)
   下面是示例:

      StringUtils.isNotEmpty(null) = false
      StringUtils.isNotEmpty("") = false
      StringUtils.isNotEmpty(" ") = true
      StringUtils.isNotEmpty("         ") = true
      StringUtils.isNotEmpty("bob") = true
      StringUtils.isNotEmpty(" bob ") = true

3. public static boolean isBlank(String str)
   判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
   下面是示例:
      StringUtils.isBlank(null) = true
      StringUtils.isBlank("") = true
      StringUtils.isBlank(" ") = true
      StringUtils.isBlank("        ") = true
      StringUtils.isBlank("\t \n \f \r") = true   //对于制表符、换行符、换页符和回车符

      StringUtils.isBlank()   //均识为空白符
      StringUtils.isBlank("\b") = false   //"\b"为单词边界符
      StringUtils.isBlank("bob") = false
      StringUtils.isBlank(" bob ") = false

4. public static boolean isNotBlank(String str)
   判断某字符串是否不为空且长度不为0且不由空白符(whitespace) 构成,等于 !isBlank(String str)
   下面是示例:

      StringUtils.isNotBlank(null) = false
      StringUtils.isNotBlank("") = false
      StringUtils.isNotBlank(" ") = false
      StringUtils.isNotBlank("         ") = false
      StringUtils.isNotBlank("\t \n \f \r") = false
      StringUtils.isNotBlank("\b") = true
      StringUtils.isNotBlank("bob") = true
      StringUtils.isNotBlank(" bob ") = true

5. public static String trim(String str)
   去掉字符串两端的控制符(control characters, char <= 32) , 如果输入为 null 则返回null
   下面是示例:
      StringUtils.trim(null) = null
      StringUtils.trim("") = ""
      StringUtils.trim(" ") = ""
      StringUtils.trim("  \b \t \n \f \r    ") = ""
      StringUtils.trim("     \n\tss   \b") = "ss"
      StringUtils.trim(" d   d dd     ") = "d   d dd"
      StringUtils.trim("dd     ") = "dd"
      StringUtils.trim("     dd       ") = "dd"

6. public static String trimToNull(String str)
   去掉字符串两端的控制符(control characters, char <= 32) ,如果变为 null 或"",则返回 null
   下面是示例:
      StringUtils.trimToNull(null) = null
      StringUtils.trimToNull("") = null
      StringUtils.trimToNull(" ") = null
      StringUtils.trimToNull("     \b \t \n \f \r    ") = null
      StringUtils.trimToNull("     \n\tss   \b") = "ss"
      StringUtils.trimToNull(" d   d dd     ") = "d   d dd"
      StringUtils.trimToNull("dd     ") = "dd"
      StringUtils.trimToNull("     dd       ") = "dd"

7. public static String trimToEmpty(String str)
   去掉字符串两端的控制符(control characters, char <= 32) ,如果变为 null 或 "" ,则返回 ""
   下面是示例:
      StringUtils.trimToEmpty(null) = ""
      StringUtils.trimToEmpty("") = ""
      StringUtils.trimToEmpty(" ") = ""
      StringUtils.trimToEmpty("     \b \t \n \f \r    ") = ""
      StringUtils.trimToEmpty("     \n\tss   \b") = "ss"
      StringUtils.trimToEmpty(" d   d dd     ") = "d   d dd"
      StringUtils.trimToEmpty("dd     ") = "dd"
      StringUtils.trimToEmpty("     dd       ") = "dd"

8. public static String strip(String str)
   去掉字符串两端的空白符(whitespace) ,如果输入为 null 则返回 null
   下面是示例(注意和 trim() 的区别):
      StringUtils.strip(null) = null
      StringUtils.strip("") = ""
      StringUtils.strip(" ") = ""
      StringUtils.strip("     \b \t \n \f \r    ") = "\b"
      StringUtils.strip("     \n\tss   \b") = "ss   \b"
      StringUtils.strip(" d   d dd     ") = "d   d dd"
      StringUtils.strip("dd     ") = "dd"
      StringUtils.strip("     dd       ") = "dd"

9. public static String stripToNull(String str)
   去掉字符串两端的空白符(whitespace) ,如果变为 null 或"",则返回 null
   下面是示例(注意和 trimToNull() 的区别):
      StringUtils.stripToNull(null) = null
      StringUtils.stripToNull("") = null
      StringUtils.stripToNull(" ") = null
      StringUtils.stripToNull("     \b \t \n \f \r    ") = "\b"
      StringUtils.stripToNull("     \n\tss   \b") = "ss   \b"
      StringUtils.stripToNull(" d   d dd     ") = "d   d dd"
      StringUtils.stripToNull("dd     ") = "dd"
      StringUtils.stripToNull("     dd       ") = "dd"

10. public static String stripToEmpty(String str)
    去掉字符串两端的空白符(whitespace) ,如果变为 null 或"" ,则返回""
    下面是示例(注意和 trimToEmpty() 的区别):
      StringUtils.stripToNull(null) = ""
      StringUtils.stripToNull("") = ""
      StringUtils.stripToNull(" ") = ""
      StringUtils.stripToNull("     \b \t \n \f \r    ") = "\b"
      StringUtils.stripToNull("     \n\tss   \b") = "ss   \b"
      StringUtils.stripToNull(" d   d dd     ") = "d   d dd"
      StringUtils.stripToNull("dd     ") = "dd"
      StringUtils.stripToNull("     dd       ") = "dd"

 

posted @ 2019-08-22 21:10  高木子  阅读(1942)  评论(1编辑  收藏  举报