Java字符串(有空就写)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35517448/article/details/82350998

字符串

字符串判空
字符串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

猜你喜欢

转载自blog.csdn.net/qq_35517448/article/details/82350998