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

ale*_*oot 36 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 && a.trim().length() <= 0);
}
Run Code Online (Sandbox Code Playgroud)

这是在Java中翻译这些方法的最佳方法吗?在Java中翻译这两种方法的最佳方法是什么?

sud*_*ode 22

我宁愿不使用String.trim来检查是否存在空格.由于它检查字符串的两端(即使在另一端找到非空格),它还会执行比您需要的更多的工作,并返回一个新的String对象.所以我更愿意实现一种方法来检查空格.

所以我的建议(如果自己实施)将如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

或者从String.trim的实现中获取提示,您可以使用字符比较而不是Character.isWhitespace():

// checking for whitespace like String.trim() does
private static boolean isWhitespace(String s) {
    int length = s.length();
    if (length > 0) {
        for (int i = 0; i < length; i++) {
            if (s.charAt(i) > ' ') {
                return false;
            }
        }
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

最后,我会考虑在每次迭代中检查字符串的两端,然后向内走.这将最小化获得答案所需的迭代次数,无论字符串的前端或末尾是否存在空格.

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果字符串为空,则IsNullOrWhitespace将返回false,这与c#的实现不匹配.这是一个实现:https://gist.github.com/jamiegs/5391961 (2认同)

Mar*_*uła 13

你总是可以通过.net反射器或其他反编译器看到c#的实现:

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

public static bool IsNullOrWhiteSpace(string value)
{
  if (value == null)
    return true;
  for (int index = 0; index < value.Length; ++index)
  {
    if (!char.IsWhiteSpace(value[index]))
      return false;
  }
  return true;
}
Run Code Online (Sandbox Code Playgroud)


Eni*_*ate 9

你可以这样试试

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)); 
  }
}
Run Code Online (Sandbox Code Playgroud)


Qwe*_*rky 6

看看StringUtilsapache commons lang 中的课程.