StringUtils.isBlank()函数

本文详细解析了Java中的StringUtils类中isBlank与isEmply方法的用法,包括如何判断字符串是否为空、非空、仅为空白字符及非空白字符等,提供了丰富的示例代码。
StringUtils的isBlank与isEmply

1. public static boolean isEmpty(String str)

  判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0

  下面是 StringUtils 判断是否为空的示例:

  StringUtils.isEmpty(null) = true

  StringUtils.isEmpty("") = true

  StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理

  StringUtils.isEmpty("   ") = false

  StringUtils.isEmpty("bob") = false

  StringUtils.isEmpty(" bob ") = false

  2. public static boolean isNotEmpty(String str)

  判断某字符串是否非空,等于 !isEmpty(String str)

  下面是示例:

  StringUtils.isNotEmpty(null) = false

  StringUtils.isNotEmpty("") = false

  StringUtils.isNotEmpty(" ") = true

  StringUtils.isNotEmpty("         ") = true

  StringUtils.isNotEmpty("bob") = true

  StringUtils.isNotEmpty(" bob ") = true

  3. public static boolean isBlank(String str)

  判断某字符串是否为空或长度为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

  4. public static boolean isNotBlank(String str)

  判断某字符串是否不为空且长度不为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

将一次性全查改为分页查询并对比。public void checkDifferences() { try { // 1. 加载并按 appCategory 分组 loadPropertiesGroupedByAppCategory(); // 2. 从 Mongo 查询所有规则(判空保护) List<NotificationRulePO> allRules = Optional .ofNullable(notificationRuleDAO.findAllNotificationRule()) .orElse(Collections.emptyList()); // 3. 遍历 Mongo 数据并对比 for (NotificationRulePO rule : allRules) { String appCategory = StringUtils.defaultString(rule.getAppCategory(), "").toLowerCase(); if (StringUtils.isBlank(appCategory)) { log.warn("Rule id={} has blank appCategory, skip", safeId(rule)); continue; } Properties props = propsByAppCategory.get(appCategory); Map<String, String> keySourceMap = keySourceMapByAppCategory.get(appCategory); if (props == null || keySourceMap == null) { log.warn("No matching properties for appCategory={}, skip rule id={}", appCategory, safeId(rule)); continue; // 跳过整个 rule } String notificationType = StringUtils.defaultString(rule.getNotificationType(), ""); if (StringUtils.isBlank(notificationType)) { log.warn("Blank notificationType for rule id={}, appCategory={}", safeId(rule), appCategory); continue; } List<org.bson.Document> copyWritings = Optional .ofNullable(rule.getCopywritingList()) .orElse(Collections.emptyList()); for (org.bson.Document doc : copyWritings) { if (doc == null) { continue; } String lang = StringUtils.defaultString(doc.getString("language"), ""); if (StringUtils.isBlank(lang)) { // 语言为空,无法构建 key,直接跳过 continue; } //根据是否去除转义字符调用normalize函数 String mongoTitle = doc.getString("title"); String mongoBody = doc.getString("body"); String titleKey = notificationType + ".title." + lang; String bodyKey = notificationType + ".body." + lang; String propTitle = findPropertyValue(props, titleKey); String propBody = findPropertyValue(props, bodyKey); // ===== Title 对比 ===== if (!StringUtils.equals(propTitle, mongoTitle)) { // 双方都空 -> 跳过 if (!(StringUtils.isBlank(propTitle) && StringUtils.isBlank(mongoTitle))) { String fullTitleKey = findFullKey(props, titleKey); String titleSourceFile = (fullTitleKey != null) ? keySourceMap.get(fullTitleKey) : null; log.warn("Title mismatch: mongoKey={}, propKey={}, properties='{}', mongo='{}', sourceFile={}", titleKey, fullTitleKey, propTitle, mongoTitle, titleSourceFile); } } // ===== Body 对比 ===== if (!StringUtils.equals(propBody, mongoBody)) { // 双方都空 -> 跳过 if (!(StringUtils.isBlank(propBody) && StringUtils.isBlank(mongoBody))) { String fullBodyKey = findFullKey(props, bodyKey); String bodySourceFile = (fullBodyKey != null) ? keySourceMap.get(fullBodyKey) : null; log.warn("Body mismatch: mongoKey={}, propKey={}, properties='{}', mongo='{}', sourceFile={}", bodyKey, fullBodyKey, propBody, mongoBody, bodySourceFile); } } } } log.info("Notification copywriting difference check completed"); } catch (IOException e) { log.error("Failed to read properties files", e); } }
10-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值