【发布时间】:2020-07-03 10:56:24
【问题描述】:
我有两个字符串
String s1 = "";
String s2 = "";
Boolean check = s1 != null && s2 != null && s1.substring(1) != null;
then return check;
为什么这段代码会抛出 StringIndexOutOfBoundException? 如果 s1 ="" 那么为什么在 s1 != null 为 false 时检查 s1.substring(1) != null ?它应该返回false而不是抛出异常? 如何解决?
【问题讨论】:
-
空字符串 "" 与
null的含义不同,它们不一样 -
对 StringUtils.isNotBlank(s1) && StringUtils.isNotBlank(s2) 进行布尔检查会更容易