c#String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的Java等价物

c#String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的Java等价物,c#,java,string,C#,Java,String,在c#中,我发现String类的两个静态方法非常有用: 我在Java中找不到有效的代理,有类似的吗 其实我是这样翻译这两种方法的: public static boolean isNullOrEmpty(String a) { return a == null || a.isEmpty(); } public static boolean isNullOrWhiteSpace(String a) { return a == null || (a.length() > 0 &am

在c#中,我发现String类的两个静态方法非常有用:

我在Java中找不到有效的代理,有类似的吗

其实我是这样翻译这两种方法的:

public static boolean isNullOrEmpty(String a) {
return a == null || a.isEmpty();
} 

public static boolean isNullOrWhiteSpace(String a) {
return a == null || (a.length() > 0 && a.trim().length() <= 0);
}
public静态布尔值isNullOrEmpty(字符串a){
返回a==null | | a.isEmpty();
} 
公共静态布尔值isNullOrWhiteSpace(字符串a){
返回a==null | |(a.length()>0&&a.trim().length()您总是可以通过.net reflector或其他反编译器看到c的实现:

public static bool IsNullOrEmpty(string value)
{
  if (value != null)
    return value.Length == 0;
  else
    return true;
}

公共静态bool IsNullOrWhiteSpace(字符串值)
{
如果(值==null)
返回true;
for(int index=0;index
看看apache commons lang中的
StringUtils
类。

apache commons lang有一套不同的字符串实用程序:


当然,如果您不想麻烦处理依赖关系,您的实现就足够了。

对于isnullorempty:returna==null | | a.length==0;
对于isnullorwhitespace,您必须检查每个字符,直到找到一个非空白字符(ascii或unicode)

您可以这样尝试

import org.apache.commons.lang.StringUtils;

public class CheckEmptyStringExample 
{  
  public static void main(String[] args)
  {
     String string1 = "";
     String string2 = "\t\r\n";
     String string3 = " ";
     String string4 = null;
     String string5 = "Hi"; 
     System.out.println("\nString one is empty? " + StringUtils.isBlank(string1));
     System.out.println("String one is not empty? " + StringUtils.isNotBlank(string1));
     System.out.println("\nString two is empty? " +  StringUtils.isBlank(string2));
     System.out.println("String two is not empty?" + StringUtils.isNotBlank(string2));
     System.out.println("\nString three is empty?" + StringUtils.isBlank(string3));
     System.out.println("String three is not empty?" + StringUtils.isNotBlank(string3));
     System.out.println("\nString four is empty?" +  StringUtils.isBlank(string4));
     System.out.println("String four is not empty?" + StringUtils.isNotBlank(string4));
     System.out.println("\nString five is empty?" + StringUtils.isBlank(string5));
     System.out.println("String five is not empty?" + StringUtils.isNotBlank(string5)); 
  }
}

apache.commons.lang.StringUtils
就是答案

我不希望使用来检查是否存在空白。它所做的工作比您需要的要多,因为它检查字符串的两端(即使在另一端发现了非空白),并返回一个新的字符串对象。因此我更希望实现一个只检查空白的方法

因此,我的建议(如果自己实施)如下:

public static boolean isNullOrEmpty(String s) {
    return s == null || s.length() == 0;
}

public static boolean isNullOrWhitespace(String s) {
    return s == null || isWhitespace(s);

}
private static boolean isWhitespace(String s) {
    int length = s.length();
    if (length > 0) {
        for (int i = 0; i < length; i++) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    return false;
}
public静态布尔值isNullOrEmpty(字符串s){
返回s==null | | s.length()==0;
}
公共静态布尔值isNullOrWhitespace(字符串s){
返回s==null | | isWhitespace(s);
}
私有静态布尔值isWhitespace(字符串s){
int length=s.length();
如果(长度>0){
for(int i=0;i
或者借鉴String.trim的实现,您可以使用字符比较而不是字符。isWhitespace()

//检查字符串之类的空白。trim()会
私有静态布尔值isWhitespace(字符串s){
int length=s.length();
如果(长度>0){
for(int i=0;i“”){
返回false;
}
}
返回true;
}
返回false;
}

最后,我会考虑在每次迭代中检查字符串的两端,向内进退。这将最小化得到答案所需的迭代次数,而不管在字符串的前端或结尾是否存在空白。< /P>

private static boolean isWhitespace(String s) {
    int length = s.length();
    if (length > 0) {
        for (int start = 0, middle = length / 2, end = length - 1; start <= middle; start++, end--) {
            if (s.charAt(start) > ' ' || s.charAt(end) > ' ') {
                return false;
            }
        }
        return true;
    }
    return false;
}
private静态布尔值isWhitespace(字符串s){
int length=s.length();
如果(长度>0){
对于(int start=0,middle=length/2,end=length-1;start''|| s.charAt(end)>''){
返回false;
}
}
返回true;
}
返回false;
}

如果导入
com.google.common.base.Strings
,则
Strings.isNullOrEmpty()


这应该更简单、更容易理解

public static boolean isNullOrEmpty(String s) {
    return s == null || s.length() == 0;
}

public static boolean isNullOrWhitespace(String s) {
    return isNullOrEmpty(s) ? true : isNullOrEmpty(s.trim());
}

JDK11中,
String
类有一个
isBlank
方法,该方法:

如果字符串为空或仅包含空格,则返回true 代码点,否则为假

虽然它不检查空值,但它肯定更接近C#的
string.IsNullOrWhiteSpace

你现在可以做:

var isNullOrWhiteSpace = str == null || str.isBlank();
或创建一个助手方法:

public static boolean isNullOrWhitespace(String s) { return s == null || str.isBlank(); }

这可能是您的答案:您的第二个实现并不完全相同。空字符串将返回
false
,而C#one返回true。您可以删除
a.length()>0&&
这将是相同的。如果字符串为空,则您的IsNullOrWhitespace实现将返回false,这与c#的实现不匹配。以下是一个实现:Jamiegs是正确的。因为您使用的是IsWhiteSpace方法,您可能会争辩“”如果不是空白而是空的,那么您可以添加一个空字符串检查。公共静态布尔值isNullOrWhitespace(string s){return s==null | | | s==“”| | isWhitespace(s)}我个人认为使用此实现的唯一原因,而不是OP建议的,是“trim”看起来,它使用的是一种不太准确的检测空白的方法。否则,这个答案就太过工程化了。我更喜欢使用更干净的代码,而不是更高效的算法(真的-只是稍微!)。@sudocode因为我没有做基准测试,所以我可以对性能做出评论,但从外观上看,这两种算法都是O(n)。因此,我不确定是否有任何优化。但我完全同意,如果OP正在开发一个库,则尽可能进行优化是有意义的。StringUtils中的哪种方法?这不是一个非常有用的答案。太简洁且没有帮助这并不能回答OP的问题,因为它不满足“isNullOrWhitespace”要求。
public static boolean isNullOrEmpty(String s) {
    return s == null || s.length() == 0;
}

public static boolean isNullOrWhitespace(String s) {
    return isNullOrEmpty(s) ? true : isNullOrEmpty(s.trim());
}
var isNullOrWhiteSpace = str == null || str.isBlank();
public static boolean isNullOrWhitespace(String s) { return s == null || str.isBlank(); }