1. 程式人生 > >java StringUtils工具類常用的方法

java StringUtils工具類常用的方法

前言:工作中看到專案組裡的大牛寫程式碼大量的用到了StringUtils工具類來做字串的操作,便學習整理了一下,方便查閱。

  • isEmpty(String str) 是否為空,空格字元為false
  • isNotEmpty(String str) 是否為非空,空格字元為true
  • isBlank(String str) 是否為空,空格字元為true
  • isNotBlank(String str) 是否為非空,空格字元為false
  • trim(String str)去除字串兩端的控制符,空字串、null 返回 null
  • trimToEmpty(String str) 去除字串兩端的控制符,空字串、null 返回""
  • stripToNull(String str) 去除字串兩端的空白符,空字串、null 返回null
  • stripToEmpty(String str) 去除字串兩端的空白符,空字串、null 返回""
  • strip(String str, String stripChars) 去掉str兩端的在stripChars中的字元

StringUtils.strip("000000134_76539000","0")="134_76539"

  • stripStart (String str,String stripChars) 去除str 前端在stripChars中的字元
  • stripEnd (String str,String stripChars) 去除str 後端在stripChars中的字元
  • equals(String str1,String str2) 比較兩個字串是否相等,如果兩個均為空則認為相等
  • indexOf(String str,char searchChar) 返回searchChar 在字串中第一次出現的位置,如果沒找到則返回 -1,如果str 為null 或者 "",也返回-1
  • indexOf(String str,char searchChar,int startPos) 返回字元searchChar從startPos開始在字串str中第一次出現的位置。
  • contains(String str,char searchChar) str中是否包含字元searchChar,str為null 或者 searchChar為null,返回false 。

StringUtils.contains("", "")  = true 

StringUtils.contains("dfg", "")  = true

  • containsIgnoreCase(String str,String searchStr) str中是否包含字元searchChar,不區分大小寫
  •  int indexOfAny(String str, char[] searchChars) 找出字元陣列searchChars中的字元第一次出現在字串str中的位置。 如果字元陣列中的字元都不在字串中,則返回-1 ,如果字串為null或"",則返回-1 
  • subString(String str,int start) 從start 開始,包含start 那個字元,得到字串str 的子串,如果start為負數,則從後面開始數起。如果str 為null 或者 "" 則返回其本身
  • subStringBefore(String str,String separator) 得到字串separator第一次出現前的子串。不包含那個字元,如果str 為null 或者 "" 則返回其本身。
  • subStringAfter(String str,String separator) 得到字串separator第一次出現後的子串,不包含那個字元,如果 str 為null,或者"",則返回其本身
  • subString(String str,int start,int end) 同上
  • left(String str,int len) 得到字串str從左邊數len長度的子串,如果str 為null 或者 "",則返回其本身,如果len小於0,則返回""
  • right(String str,int len)得到字串str從右邊數len長度的子串
  • mid(String str,int pos,int len) 得到字串str從pos開始len長度的子串,pos小於0,則設為0。
  • split(String str) 把字串拆分成一個字串陣列,用空白符 作為分隔符,字串為null 返回null,字串為"",返回空陣列{}
  • split(String str,char c) 按照 char c 拆分字串
  • join(Object[] arrey)把陣列中的元素連線成一個字串返回
  • join(Object[] arrey,char c) 把陣列中的元素拼接成一個字串返回,把分隔符 c 也帶上
  • deleteWhitespace(String str) 刪除字串中的所有空白符,包括轉義字元
  • removeStart(String str,String remove) 如果字串str是以remove開始,則去掉這個開始,然後返回,否則返回原來的串
  • removeEnd(String str,String remove) 如果字串str是以字串remove結尾,則去掉這個結尾,然後返回,否則返回原來的串。
  • remove(String str,char remove) 去掉字串str中所有包含remove的部分,然後返回
  • replace(String str,String reql,String with) 在字串text中用with代替repl,替換所有
  • replaceChars(String str,char old,char new) 在字串中 new 字元代替 old 字元
  • replaceChars(String str, String searchChars, String replaceChars) 這個有點特別,先看下面三個例子

StringUtils.replaceChars("asssdf","s","yyy"))    =    "ayyydf" 

StringUtils.replaceChars("asdf","sd","y"))        = "ayf" 

StringUtils.replaceChars("assssddddf","sd","y"))= "ayyyyf"

解釋:為什麼會出現上面這樣的結果呢?原來這個置換規則是這樣的,他是拿searchChars的index,去replaceChars找相應的index然後替換掉,怎麼說呢?比如說第一個例子 s 的index 是0,找到yyy相對應的index為0的字元是y。第二個例子 's' 的index是0,'d'的index是1, 字元's' 可以找到對應index為0的 'y',d就找不到index為'1'的的字元了,所以就直接過濾掉了,聽明白了嗎?

  • overlay(String str,String new,int start,int end) 用字串new 覆蓋字串str從start 到 end 之間的串
  • chop(String str) 去掉字串的最後一個字元,比如/r/n
  • repeat(String str,int repart) 重複字串repeat次
  • rightPad(String str,int size,String padStr) size長度的字串,如果不夠用padStr補齊
  • leftPad(String str,int size,String padStr)同上
  • center(String str,int size)產生一個字串,長度等於size,str位於新串的中心
  • swapCase(String str) 字串中的大寫轉小寫,小寫轉換為大寫