检查Java中的字符串是空还是空[重复]

本文翻译自:Checking if a string is empty or null in Java [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I'm parsing HTML data. 我正在解析HTML数据。 The String may be null or empty, when the word to parse does not match. 当要解析的单词不匹配时, String可以为null或为空。

So, I wrote it like this: 所以,我这样写了:

if(string.equals(null) || string.equals("")){
    Log.d("iftrue", "seem to be true");
}else{
    Log.d("iffalse", "seem to be false");
}

When I delete String.equals("") , it does not work correctly. 当我删除String.equals("") ,它无法正常工作。

I thought String.equals("") wasn't correct. 我以为String.equals("")不正确。

How can I best check for an empty String ? 我怎样才能最好地检查空String


#1楼

参考:https://stackoom.com/question/zlht/检查Java中的字符串是空还是空-重复


#2楼

You can leverage Apache Commons StringUtils.isEmpty(str) , which checks for empty strings and handles null gracefully. 您可以利用Apache Commons StringUtils.isEmpty(str) ,它检查空字符串并优雅地处理null

Example: 例:

System.out.println(StringUtils.isEmpty("")); // true
System.out.println(StringUtils.isEmpty(null)); // true

Google Guava also provides a similar, probably easier-to-read method: Strings.isNullOrEmpty(str) . Google Guava还提供了一种类似的,可能更容易阅读的方法: Strings.isNullOrEmpty(str)

Example: 例:

System.out.println(Strings.isNullOrEmpty("")); // true
System.out.println(Strings.isNullOrEmpty(null)); // true

#3楼

检查null或空的正确方法如下:

if(str != null && !str.isEmpty()) { /* do your stuffs here */ }

#4楼

You can use Apache commons-lang 您可以使用Apache commons-lang

StringUtils.isEmpty(String str) - Checks if a String is empty ("") or null. StringUtils.isEmpty(String str) - 检查String是否为空(“”)或null。

or 要么

StringUtils.isBlank(String str) - Checks if a String is whitespace, empty ("") or null. StringUtils.isBlank(String str) - 检查String是否为空格,空(“”)或null。

the latter considers a String which consists of spaces or special characters eg " " empty too. 后者考虑一个由空格或特殊字符组成的字符串,例如“”也是空的。 See java.lang.Character.isWhitespace API 请参阅java.lang.Character.isWhitespace API


#5楼

import com.google.common.base

if(!Strings.isNullOrEmpty(String str)) {
   // Do your stuff here 
}

#6楼

This way you check if the string is not null and not empty, also considering the empty spaces: 这样你检查字符串是否为空而不是空,也考虑空格:

boolean isEmpty = str == null || str.trim().length() == 0;
if (isEmpty) {
    // handle the validation
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java删除字符串的所有相邻重复项,可以使用StringBuilder类来处理。 思路如下: 1. 创建一个StringBuilder对象,并将原始字符串添加到该对象。 2. 遍历StringBuilder对象的字符,从第一个字符开始。 3. 检查当前字符是否和下一个字符相同。如果相同,则删除当前字符和下一个字符。 4. 继续检查下一个字符,直到遍历完所有的字符。 5. 将StringBuilder对象转换为最终的字符串结果。 以下是一个示例代码实现: ```java public class Main { public static void main(String[] args) { String str = "aaabbbcccdddeee"; String result = removeAdjacentDuplicates(str); System.out.println(result); } public static String removeAdjacentDuplicates(String str) { StringBuilder sb = new StringBuilder(str); int index = 0; while (index < sb.length() - 1) { if (sb.charAt(index) == sb.charAt(index + 1)) { sb.delete(index, index + 2); } else { index++; } } return sb.toString(); } } ``` 在上述代码,我们定义了一个`removeAdjacentDuplicates`方法,该方法接收一个字符串作为参数,并返回删除相邻重复项后的字符串。 通过遍历StringBuilder对象的字符,我们可以检查当前字符是否和后一个字符相同,并根据情况删除字符或者继续检查下一个字符。最后,我们将StringBuilder对象转换为最终的字符串结果并返回。 在示例代码字符串"aaabbbcccdddeee"会被转换为"abcd"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值