Class StringUtil

java.lang.Object
com.acanx.utils.StringUtil

public class StringUtil extends Object
ACANX-Util / com.acanx.utils / StringUtil 文件由 ACANX 创建于 2019/1/5 . 15:46 StringUtil:字符串处理相关工具类 补充说明: 2019/1/5 15:46
Since:
0.0.1
Version:
v0.0.1.0
Author:
ACANX
  • Method Details

    • isEmpty

      @Alpha public static boolean isEmpty(String str)

      Checks if a CharSequence is empty ("") or null.

       StringUtil.isEmpty(null)      = true
       StringUtil.isEmpty("")        = true
       StringUtil.isEmpty(" ")       = false
       StringUtil.isEmpty("bob")     = false
       StringUtil.isEmpty("  bob  ") = false
       
      Parameters:
      str - 字符串
      Returns:
      空值判断结果
      Since:
      0.0.1.10
    • isNotEmpty

      @Alpha public static boolean isNotEmpty(String str)

      Checks if a CharSequence is not empty ("") and not null.

       StringUtil.isNotEmpty(null)      = false
       StringUtil.isNotEmpty("")        = false
       StringUtil.isNotEmpty(" ")       = true
       StringUtil.isNotEmpty("bob")     = true
       StringUtil.isNotEmpty("  bob  ") = true
       
      Parameters:
      str - 字符串
      Returns:
      判断结果
      Since:
      0.0.1.10
    • isBlank

      @Alpha public static boolean isBlank(String str)
      字符串空格判断
      Parameters:
      str - 字符串
      Returns:
      判断结果
      Since:
      0.0.1.10
    • isNotBlank

      @Alpha public static boolean isNotBlank(String str)
       StringUtil.isNotBlank(null)      = false
       StringUtil.isNotBlank("")        = false
       StringUtil.isNotBlank(" ")       = false
       StringUtil.isNotBlank("bob")     = true
       StringUtil.isNotBlank("  bob  ") = true
       
      Parameters:
      str - 字符串
      Returns:
      非空格判断结果
      Since:
      0.0.1.10
    • isWhitespace

      @Alpha public static boolean isWhitespace(CharSequence cs)

      Checks if the CharSequence contains only whitespace.

      Whitespace is defined by Character.isWhitespace(char).

      null will return false. An empty CharSequence (length()=0) will return true.

       StringUtil.isWhitespace(null)   = false
       StringUtil.isWhitespace("")     = true
       StringUtil.isWhitespace("  ")   = true
       StringUtil.isWhitespace("abc")  = false
       StringUtil.isWhitespace("ab2c") = false
       StringUtil.isWhitespace("ab-c") = false
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if only contains whitespace, and is non-null
      Since:
      2.0, 3.0 Changed signature from isWhitespace(String) to isWhitespace(CharSequence)
    • equals

      @Alpha public static boolean equals(String str1, String str2)
      字符串相同判断
      Parameters:
      str1 - 字符串1
      str2 - 字符串2
      Returns:
      判断结果
      Since:
      0.0.1.10
    • join

      @Alpha public static String join(Object[] array, String delimiter)
      Joins the elements of the provided array into a single String containing the provided list of elements.

      No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

       StringUtil.join(null, *)                = null
       StringUtil.join([], *)                  = ""
       StringUtil.join([null], *)              = ""
       StringUtil.join(["a", "b", "c"], "--")  = "a--b--c"
       StringUtil.join(["a", "b", "c"], null)  = "abc"
       StringUtil.join(["a", "b", "c"], "")    = "abc"
       StringUtil.join([null, "", "a"], ',')   = ",,a"
       
      Parameters:
      array - the array of values to join together, may be null
      delimiter - the separator character to use, null treated as ""
      Returns:
      the joined String, null if null array input
    • join

      @Alpha public static String join(Object[] array, String delimiter, int startIndex, int endIndex)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

       StringUtil.join(null, *, *, *)                = null
       StringUtil.join([], *, *, *)                  = ""
       StringUtil.join([null], *, *, *)              = ""
       StringUtil.join(["a", "b", "c"], "--", 0, 3)  = "a--b--c"
       StringUtil.join(["a", "b", "c"], "--", 1, 3)  = "b--c"
       StringUtil.join(["a", "b", "c"], "--", 2, 3)  = "c"
       StringUtil.join(["a", "b", "c"], "--", 2, 2)  = ""
       StringUtil.join(["a", "b", "c"], null, 0, 3)  = "abc"
       StringUtil.join(["a", "b", "c"], "", 0, 3)    = "abc"
       StringUtil.join([null, "", "a"], ',', 0, 3)   = ",,a"
       
      Parameters:
      array - the array of values to join together, may be null
      delimiter - the separator character to use, null treated as ""
      startIndex - the first index to start joining from.
      endIndex - the index to stop joining from (exclusive).
      Returns:
      the joined String, null if null array input; or the empty string if endIndex - startIndex <= 0. The number of joined entries is given by endIndex - startIndex
      Throws:
      ArrayIndexOutOfBoundsException - ife
      startIndex < 0 or
      startIndex >= array.length() or
      endIndex < 0 or
      endIndex > array.length()
    • join

      @Alpha public static String join(Object[] array, char delimiter, int startIndex, int endIndex)
      join
      Parameters:
      array - array
      delimiter - delimiter
      startIndex - 起始位置
      endIndex - 结束位置
      Returns:
      结果
    • join

      @Alpha public static String join(List<?> list, String separator, int startIndex, int endIndex)

      Joins the elements of the provided List into a single String containing the provided list of elements.

      No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.

       StringUtil.join(null, *)               = null
       StringUtil.join([], *)                 = ""
       StringUtil.join([null], *)             = ""
       StringUtil.join(["a", "b", "c"], ';')  = "a;b;c"
       StringUtil.join(["a", "b", "c"], null) = "abc"
       StringUtil.join([null, "", "a"], ';')  = ";;a"
       
      Parameters:
      list - the List of values to join together, may be null
      separator - the separator character to use
      startIndex - the first index to start joining from. It is an error to pass in a start index past the end of the list
      endIndex - the index to stop joining from (exclusive). It is an error to pass in an end index past the end of the list
      Returns:
      the joined String, null if null list input
      Since:
      3.8
    • join

      @Alpha public static String join(Iterator<?> iterator, String separator)

      Joins the elements of the provided Iterator into a single String containing the provided elements.

      No delimiter is added before or after the list. A null separator is the same as an empty String ("").

      See the examples here: join(Object[],String).

      Parameters:
      iterator - the Iterator of values to join together, may be null
      separator - the separator character to use, null treated as ""
      Returns:
      the joined String, null if null iterator input
    • join

      @Alpha public static String join(int[] array, char separator)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.

       StringUtil.join(null, *)               = null
       StringUtil.join([], *)                 = ""
       StringUtil.join([null], *)             = ""
       StringUtil.join([1, 2, 3], ';')  = "1;2;3"
       StringUtil.join([1, 2, 3], null) = "123"
       
      Parameters:
      array - the array of values to join together, may be null
      separator - the separator character to use
      Returns:
      the joined String, null if null array input
      Since:
      3.2
    • join

      @Alpha public static String join(int[] array, char delimiter, int startIndex, int endIndex)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.

       StringUtil.join(null, *)               = null
       StringUtil.join([], *)                 = ""
       StringUtil.join([null], *)             = ""
       StringUtil.join([1, 2, 3], ';')  = "1;2;3"
       StringUtil.join([1, 2, 3], null) = "123"
       
      Parameters:
      array - the array of values to join together, may be null
      delimiter - the separator character to use
      startIndex - the first index to start joining from. It is an error to pass in a start index past the end of the array
      endIndex - the index to stop joining from (exclusive). It is an error to pass in an end index past the end of the array
      Returns:
      the joined String, null if null array input
      Since:
      3.2
    • join

      @Alpha @SafeVarargs public static <T> String join(T... elements)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      No separator is added to the joined String. Null objects or empty strings within the array are represented by empty strings.

       StringUtil.join(null)            = null
       StringUtil.join([])              = ""
       StringUtil.join([null])          = ""
       StringUtil.join(["a", "b", "c"]) = "abc"
       StringUtil.join([null, "", "a"]) = "a"
       
      Type Parameters:
      T - the specific type of values to join together
      Parameters:
      elements - the values to join together, may be null
      Returns:
      the joined String, null if null array input
      Since:
      2.0, 3.0 Changed signature to use varargs
    • isNumeric

      @Alpha public static boolean isNumeric(String str)
       StringUtil.isNumeric(null)   = false
       StringUtil.isNumeric("")     = false
       StringUtil.isNumeric("  ")   = false
       StringUtil.isNumeric("123")  = true
       StringUtil.isNumeric("१२३")  = true
       StringUtil.isNumeric("12 3") = false
       StringUtil.isNumeric("ab2c") = false
       StringUtil.isNumeric("12-3") = false
       StringUtil.isNumeric("12.3") = false
       StringUtil.isNumeric("-123") = false
       StringUtil.isNumeric("+123") = false
       
      Parameters:
      str - 字符串
      Returns:
      判断结果
    • isAlpha

      @Alpha public static boolean isAlpha(CharSequence cs)

      Checks if the CharSequence contains only Unicode letters.

      null will return false. An empty CharSequence (length()=0) will return false.

       StringUtil.isAlpha(null)   = false
       StringUtil.isAlpha("")     = false
       StringUtil.isAlpha("  ")   = false
       StringUtil.isAlpha("abc")  = true
       StringUtil.isAlpha("ab2c") = false
       StringUtil.isAlpha("ab-c") = false
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if only contains letters, and is non-null
      Since:
      3.0 Changed signature from isAlpha(String) to isAlpha(CharSequence), 3.0 Changed "" to return false and not true
    • isEmpty

      @Alpha public static boolean isEmpty(CharSequence cs)

      Checks if a CharSequence is empty ("") or null.

       StringUtil.isEmpty(null)      = true
       StringUtil.isEmpty("")        = true
       StringUtil.isEmpty(" ")       = false
       StringUtil.isEmpty("bob")     = false
       StringUtil.isEmpty("  bob  ") = false
       

      NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().

      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is empty or null
      Since:
      3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
    • isAlphanumeric

      @Alpha public static boolean isAlphanumeric(String str)
       StringUtil.isAlphanumeric(null)   = false
       StringUtil.isAlphanumeric("")     = false
       StringUtil.isAlphanumeric("  ")   = false
       StringUtil.isAlphanumeric("abc")  = true
       StringUtil.isAlphanumeric("ab c") = false
       StringUtil.isAlphanumeric("ab2c") = true
       StringUtil.isAlphanumeric("ab-c") = false
       
      Parameters:
      str - 字符串
      Returns:
      判断结果
    • getStringSHA1Code

      @Alpha public static String getStringSHA1Code(String input)
      获取字符串的SHA1值
      Parameters:
      input - 字符串
      Returns:
      SHA1
      Since:
      0.0.1.10
    • getStringMD5Code

      @Alpha public static String getStringMD5Code(String str)
      字符串的MD5加密 默认输出为大写十六进制字符
      Parameters:
      str - 待加密的字符串
      Returns:
      加密后的MD5值,32位(十六进制字符、大写)
    • getStrSeq

      @Alpha public static String getStrSeq(String str, int length)
      生成指定长度的指定字符串 如: getStrSeq("123", 3) return "123123123" getStrSeq("321", 4) return "321321321321"
      Parameters:
      str - String 指定字符串
      length - int 循环长度
      Returns:
      String 生成的字符串
    • underlineToCamelCase

      public static String underlineToCamelCase(String underlineString)
      将下划线字符串转换为驼峰字符串
      Parameters:
      underlineString - 下划线字符串
      Returns:
      小驼峰格式的字符串
    • camelCaseToUnderline

      @Alpha public static String camelCaseToUnderline(String camelString)
      将下划线字符串转换为驼峰字符串
      Parameters:
      camelString - 小驼峰字符串
      Returns:
      下划线格式的字符串
    • camelToSplitName

      @Alpha public static String camelToSplitName(String camelName, String split)
      驼峰转下划线
      Parameters:
      camelName - 驼峰命名字符串
      split - split
      Returns:
      转换后的结果字符串
    • splitNameByLastCamel

      @Alpha public static String splitNameByLastCamel(String camelName)
      splitNameByLastCamel
      Parameters:
      camelName - 驼峰格式的字符串
      Returns:
      转换后的结果
    • toUpperCaseFirstChar

      @Alpha public static String toUpperCaseFirstChar(String value)
      字符串首字母转大写
      Parameters:
      value - 字符串
      Returns:
      转换后的结果字符串
    • right

      @Alpha public static String right(String str, int num)
      字符串左
      Parameters:
      str - 字符串
      num - num
      Returns:
      处理后的结果
    • right

      @Alpha public static String right(String str1, int num, String str2)
      right
      Parameters:
      str1 - 字符串
      num - num
      str2 - str2
      Returns:
      处理结果
    • left

      @Alpha public static String left(String str, int num)
      left
      Parameters:
      str - 字符串
      num - num
      Returns:
      处理后的结果
    • left

      @Alpha public static String left(String str1, int num, String str2)
      left
      Parameters:
      str1 - 字符串1
      num - num
      str2 - 字符串2
      Returns:
      处理结果
    • defaultIfBlank

      @Alpha public static String defaultIfBlank(String str)
      defaultIfBlank
      Parameters:
      str - 字符串
      Returns:
      处理结果
    • leftDelChar

      @Alpha public static String leftDelChar(String descStr, String text, char ch)
      leftDelChar
      Parameters:
      descStr - descStr
      text - text
      ch - 字符
      Returns:
      结果
    • rightPad

      @Alpha public static String rightPad(String str, int size, char padChar)

      Right pad a String with a specified character.

      The String is padded to the size of size.

       StringUtil.rightPad(null, *, *)     = null
       StringUtil.rightPad("", 3, 'z')     = "zzz"
       StringUtil.rightPad("bat", 3, 'z')  = "bat"
       StringUtil.rightPad("bat", 5, 'z')  = "batzz"
       StringUtil.rightPad("bat", 1, 'z')  = "bat"
       StringUtil.rightPad("bat", -1, 'z') = "bat"
       
      Parameters:
      str - the String to pad out, may be null
      size - the size to pad to
      padChar - the character to pad with
      Returns:
      right padded String or original String if no padding is necessary, null if null String input
      Since:
      0.0.1.10
    • rightPad

      @Alpha public static String rightPad(String str, int size, String padStr)

      Right pad a String with a specified String.

      The String is padded to the size of size.

       StringUtil.rightPad(null, *, *)      = null
       StringUtil.rightPad("", 3, "z")      = "zzz"
       StringUtil.rightPad("bat", 3, "yz")  = "bat"
       StringUtil.rightPad("bat", 5, "yz")  = "batyz"
       StringUtil.rightPad("bat", 8, "yz")  = "batyzyzy"
       StringUtil.rightPad("bat", 1, "yz")  = "bat"
       StringUtil.rightPad("bat", -1, "yz") = "bat"
       StringUtil.rightPad("bat", 5, null)  = "bat  "
       StringUtil.rightPad("bat", 5, "")    = "bat  "
       
      Parameters:
      str - the String to pad out, may be null
      size - the size to pad to
      padStr - the String to pad with, null or empty treated as single space
      Returns:
      right padded String or original String if no padding is necessary, null if null String input
    • leftPad

      @Alpha public static String leftPad(String str, int size, char padChar)

      Left pad a String with a specified character.

      Pad to a size of size.

       StringUtil.leftPad(null, *, *)     = null
       StringUtil.leftPad("", 3, 'z')     = "zzz"
       StringUtil.leftPad("bat", 3, 'z')  = "bat"
       StringUtil.leftPad("bat", 5, 'z')  = "zzbat"
       StringUtil.leftPad("bat", 1, 'z')  = "bat"
       StringUtil.leftPad("bat", -1, 'z') = "bat"
       
      Parameters:
      str - the String to pad out, may be null
      size - the size to pad to
      padChar - the character to pad with
      Returns:
      left padded String or original String if no padding is necessary, null if null String input
      Since:
      2.0
    • leftPad

      @Alpha public static String leftPad(String str, int size, String padStr)

      Left pad a String with a specified String.

      Pad to a size of size.

       StringUtil.leftPad(null, *, *)      = null
       StringUtil.leftPad("", 3, "z")      = "zzz"
       StringUtil.leftPad("bat", 3, "yz")  = "bat"
       StringUtil.leftPad("bat", 5, "yz")  = "yzbat"
       StringUtil.leftPad("bat", 8, "yz")  = "yzyzybat"
       StringUtil.leftPad("bat", 1, "yz")  = "bat"
       StringUtil.leftPad("bat", -1, "yz") = "bat"
       StringUtil.leftPad("bat", 5, null)  = "  bat"
       StringUtil.leftPad("bat", 5, "")    = "  bat"
       
      Parameters:
      str - the String to pad out, may be null
      size - the size to pad to
      padStr - the String to pad with, null or empty treated as single space
      Returns:
      left padded String or original String if no padding is necessary, null if null String input
    • positionOf

      @Alpha public static int positionOf(String str, char searchChar)
      字符在字符串中的位置
      Parameters:
      str - 字符串
      searchChar - 搜索字符
      Returns:
      位置结果
    • positionOf

      @Alpha public static int positionOf(String str, String searchStr)
      子字符串在字符串中的位置
      Parameters:
      str - 字符串
      searchStr - 子字符串
      Returns:
      位置结果
    • containsAll

      @Alpha public static boolean containsAll(String str, String searchChars)
      containsAll
      Parameters:
      str - str
      searchChars - searchChars
      Returns:
      结果
    • contains

      @Alpha public static boolean contains(CharSequence seq, CharSequence searchSeq)

      Checks if CharSequence contains a search CharSequence, handling null. This method uses String.indexOf(String) if possible.

      A null CharSequence will return false.

       StringUtil.contains(null, *)     = false
       StringUtil.contains(*, null)     = false
       StringUtil.contains("", "")      = true
       StringUtil.contains("abc", "")   = true
       StringUtil.contains("abc", "a")  = true
       StringUtil.contains("abc", "z")  = false
       
      Parameters:
      seq - 被检索的字符串
      searchSeq - 检索的子字符串
      Returns:
      结果
    • substring

      @Alpha public static String substring(String str, int start, int end)

      Gets a substring from the specified String avoiding exceptions.

      A negative start position can be used to start/end n characters from the end of the String.

      The returned substring starts with the character in the start position and ends before the end position. All position counting is zero-based -- i.e., to start at the beginning of the string use start = 0. Negative start and end positions can be used to specify offsets relative to the end of the String.

      If start is not strictly to the left of end, "" is returned.

       StringUtil.substring(null, *, *)    = null
       StringUtil.substring("", * ,  *)    = "";
       StringUtil.substring("abc", 0, 2)   = "ab"
       StringUtil.substring("abc", 2, 0)   = ""
       StringUtil.substring("abc", 2, 4)   = "c"
       StringUtil.substring("abc", 4, 6)   = ""
       StringUtil.substring("abc", 2, 2)   = ""
       StringUtil.substring("abc", -2, -1) = "b"
       StringUtil.substring("abc", -4, 2)  = "ab"
       
      Parameters:
      str - the String to get the substring from, may be null
      start - the position to start from, negative means count back from the end of the String by this many characters
      end - the position to end at (exclusive), negative means count back from the end of the String by this many characters
      Returns:
      substring from start position to end position, null if null String input
    • substring

      @Alpha public static String substring(String str1, int pos, int len, String str2)
      substring
      Parameters:
      str1 - 字符串
      pos - 起始位置
      len - 长度
      str2 - 字符串2
      Returns:
      结果
    • substringEquals

      @Alpha public static boolean substringEquals(String str1, int pos, int len, String str2)
      substringEquals
      Parameters:
      str1 - str1
      pos - 位置
      len - 长度
      str2 - str2
      Returns:
      结果
    • listToString

      @Alpha public static String listToString(List<String> list, boolean wrappeFlag, char separator)
      集合转字符串(以separator(如逗号)间隔)
      Parameters:
      list - 字符串集合
      wrappeFlag - 首末是否需要添加间隔符
      separator - 间隔符
      Returns:
      结果字符串
    • stringToList

      @Alpha public static List<String> stringToList(String str)
      元素之间以逗号间隔的字符串转集合
      Parameters:
      str - 字符串
      Returns:
      字符串集合
    • getCurrentDateTimeString

      @Alpha public static String getCurrentDateTimeString(SimpleDateFormat sdf)
      获取当前日期时间字符串
      Parameters:
      sdf - SimpleDateFormat
      Returns:
      格式化后的当前日期时间字符串
    • formattedDateStringToDate

      @Alpha public static Date formattedDateStringToDate(String dateStr, String dateFormat)
      将String字符串转换为java.util.Date格式日期
      Parameters:
      dateStr - 表示日期的字符串
      dateFormat - 传入字符串的日期表示格式(如:"yyyy-MM-dd HH:mm:ss")
      Returns:
      java.util.Date类型日期对象(如果转换失败则返回null)
    • formattedDateStrToSqlDate

      @Alpha public static Timestamp formattedDateStrToSqlDate(String dateStr, String dateFormat)
      将String字符串转换为java.sql.Timestamp格式日期,用于数据库保存
      Parameters:
      dateStr - 表示日期的字符串
      dateFormat - 传入字符串的日期表示格式(如:"yyyy-MM-dd HH:mm:ss")
      Returns:
      java.sql.Timestamp类型日期对象(如果转换失败则返回null)
    • formattedDateStringToTimestamp

      @Alpha public static Timestamp formattedDateStringToTimestamp(String dateStr, String format)
      日期时间字符串转Timestamp
      Parameters:
      dateStr - 日期字符串
      format - 格式化模板
      Returns:
      Timestamp
    • formattedTimeStringToLong

      @Alpha public static Long formattedTimeStringToLong(String time, String format)
      将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
      Parameters:
      time - 时间
      format - 时间格式化模式
      Returns:
      Long类型的时间戳
    • timestampStrToFormattedDateString

      @Alpha public static String timestampStrToFormattedDateString(String seconds, String format)
      秒级的时间戳转Date
      Parameters:
      seconds - 秒级的时间戳
      format - 格式化
      Returns:
      日期时间字符串
    • containsString

      public static boolean containsString(String inputString, List<String> stringList)
      判断输入的字符串是否是给定字符串列表中的某一个等值的字符串
      Parameters:
      inputString - 需要判断的字符串
      stringList - 字符串列表
      Returns:
      如果输入的字符串在列表中,则返回 true;否则返回 false