[英]NullReferenceException in string.IsNullOrWhiteSpace() and string.IsNullOrEmpty()
[英]Java equivalent of c# String.IsNullOrEmpty() and String.IsNullOrWhiteSpace()
在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);
}
這是在Java中翻譯這些方法的最佳方法嗎? 在Java中翻譯這兩種方法的最佳方法是什么?
我寧願不使用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;
}
或者從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;
}
最后,我會考慮在每次迭代中檢查字符串的兩端,然后向內走。 這將最小化獲得答案所需的迭代次數,無論字符串的前端或末尾是否存在空格。
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;
}
你總是可以通過.net反射器或其他反編譯器看到c#的實現:
public static bool IsNullOrEmpty(string value)
{
if (value != null)
return value.Length == 0;
else
return true;
}
和
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;
}
你可以這樣試試
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
類。
如果您導入com.google.common.base.Strings
,則會有Strings.isNullOrEmpty()
isNullOrEmpty(@Nullable String string)
Apache Commons Lang為字符串提供了各種方便的實用程序: http : //commons.apache.org/lang/api-release/org/apache/commons/lang3/StringUtils.html
當然,如果您不想打擾依賴項,那么您的實現就足夠了。
這應該更簡單易懂。
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,否則返回false。
雖然它沒有檢查無效,但它肯定更接近C#的string.IsNullOrWhiteSpace
。
你現在可以這樣做:
var isNullOrWhiteSpace = str == null || str.isBlank();
或者創建一個幫助方法:
public static boolean isNullOrWhitespace(String s) { return s == null || str.isBlank(); }
for isnullorempty:return a == null || a.length == 0;
對於isnullorwhitespace,您必須檢查每個單個字符,直到找到非空白字符(ascii或unicode)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.