자바 문자열 도구 클래스 (지속 업데이트)

package tmbf.nothing.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;

/**
 * @ClassName:
 * @Description:       
 * @author yanbxa
 * @date
 */
public class StringUtil {

  /**
   *@Title: isEmptyString
   *@Description:           :NULL、    、"null"、"NULL"、
   *@Author: yanbxa
   *@Param: [value]
   *@Return: boolean
   *@Date: 2019/11/26 10:42
   **/
  public static boolean isEmptyString(String value) {
    if (value == null) { // NULL
      return true;
    }
    if (value.trim().length() < 1) { //     
      return true;
    }
    if ("null".equalsIgnoreCase(value.trim())) { // "null"、"NULL"
      return true;
    }
    return false;
  }

  /**
   *@Title: splitStrToArrayByLength
   *@Description:                    
   *@Author: yanbxa
   *@Param: [str, eachLength]
   *@Return: java.lang.String[]
   *@Date: 2019/11/26 10:55
   **/
  public static String[] splitStrToArrayByLength(String str, int eachLength) {
    if (eachLength < 1) {
      return new String[] { str };
    }
    int remainder = str.length() % eachLength; //   
    int merchant = str.length() / eachLength; //  
    int arraySize = merchant;
    if (remainder > 0) {
      arraySize = arraySize + 1;
    }
    String[] array = new String[arraySize];
    for (int i = 0; i < arraySize; i++) {
      int startIndex = i * eachLength;
      int endIndex;
      if (i < arraySize - 1) {
        endIndex = (i + 1) * eachLength;
      } else {
        endIndex = str.length();
      }
      array[i] = str.substring(startIndex, endIndex);
    }
    return array;
  }

  /**
   *@Title: toJSONStringWithNullValue
   *@Description:      JSON   [map value null    key、string   null  ""    、null      0]
   *@Author: yanbxa
   *@Param: [object]
   *@Return: java.lang.String
   *@Date: 2019/11/26 10:50
   **/
  public static String toJSONStringWithNullValue(Object object) {
    return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,
      SerializerFeature.WriteNullNumberAsZero);
  }

  /**
   *@Title: ClobToString
   *@Description:     String
   *@Author: yanbxa
   *@Param: [clob]
   *@Return: java.lang.String
   *@Date: 2019/12/2 10:52
   **/
  public static String ClobToString(Clob clob) {
    String reString = "";
    Reader is = null;
    BufferedReader br = null;
    try {
      is = clob.getCharacterStream();
      br = new BufferedReader(is);
      String s = br.readLine();
      StringBuffer sb = new StringBuffer();
      while (s != null) {
        sb.append(s);
        sb.append("\r
"); // s = br.readLine(); } reString = sb.toString(); } catch (Exception e) { return reString; } finally { try { if (br != null) { br.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (is != null) { is.close(); } } catch (Exception e) { e.printStackTrace(); } } return reString; } public static void main(String[] args) { // System.out.println(isEmptyString(null)); System.out.println(isEmptyString(" ")); System.out.println(isEmptyString("null")); System.out.println(toJSONStringWithNullValue(new String[] { "123", "234" })); System.out.println(toJSONStringWithNullValue(splitStrToArrayByLength("123456789", 4))); } }

좋은 웹페이지 즐겨찾기