Java字串(有空就寫)
阿新 • • 發佈:2019-02-03
字串
#####字串判空
######字串isEmpty
StringUtil.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空處理
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
######判斷字串非空
StringUtil.isNotEmpty(null) = false; StringUtil.isNotEmpty("") = false; StringUtil.isNotEmpty(" ") = true; StringUtil.isNotEmpty("str") = true; StringUtil.isNotEmpty("str ") = true;
######判斷某字串是否為空或長度為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
######判斷某字串是否不為空且長度不為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
#####Java往字串中加入某字元
String a = "hello";
StringBuffer sb = new StringBuffer();
1、
sb.append(a).insert(2,"aaa"); //在第3個之前新增
結果sb.toSring()為"heaaallo"
2、
sb.append(a).replace(1, 3, "aaa"); //從第2開始,到第4個結束
結果sb.toSring()為"haaalo"
2、
sb.append(a).delete(1, 3);//從第2開始,到第4個結束
結果sb.toSring()為"hlo"
######字串截掉
String str = "hello";
str.substring(0,1)---->h
範圍是[0,1)
######字串換掉指定字元
String str = "hello";
str.replace("e", "");---->hllo
######字串轉date
import java.sql.Date;
String str = "2018年9月5日";
String dateStr = str.replace("年", "-").replace("月", "-").repalce("日", "");
Date ggdate = Date.valueOf(gonggaoriqi);----->可存入到資料庫的格式
######判斷兩個字串是否相同
String str = "abc";
String str2 = "abc";
String str3 = "bhn";
str.equals(str2)----->true
str.equals(str3)------> flase
- 擷取後面的字串:
string.substring(columnEntity.getUrl().lastIndexOf("/"));
擷取前面的字串:
string.substring(0,columnEntity.getUrl().lastIndexOf("/"))