[英]What is the right approach to concatenating a null String in Java?
我知道以下內容:
String s = null;
System.out.println("s: " + s);
將輸出: s: null
。
我如何讓它只輸出s:
在我的情況下,這很重要,因為我必須從4個值, s1, s2, s3, s4
連接String
, 其中每個值可能有也可能沒有 null
值 。
我問這個問題因為我不想檢查s1到s4的每個組合 (也就是檢查這些變量是否為null
)或者在末尾用empty
String
替換"null"
,因為我認為可能有一些更好的方法。
最簡潔的解決方案是:
System.out.println("s: " + (s == null ? "" : s));
或者也許創建或使用靜態幫助方法來做同樣的事情; 例如
System.out.println("s: " + denull(s));
但是,這個問題具有過度使用/濫用null
的應用程序的“氣味”。 這是更好地只使用/返回null
,如果它有特定的含義是不同的(並且需要是不同的)非空值的含義。
例如:
null
String屬性,請考慮將它們顯式初始化為""
。 null
來表示空數組或集合。 null
。 現在顯然所有這些都有反例,有時你必須處理一個預先存在的API,無論出於什么原因,它都會給你空值。 但是,根據我的經驗,最好避免使用null
...大部分時間。
因此,在您的情況下,更好的方法可能是:
String s = ""; /* instead of null */
System.out.println("s: " + s);
您可以在Java 8中使用以下內容:
String s1 = "a";
String s2 = null;
String s3 = "c";
String s4 = "d";
String concat = Stream.of(s1, s2, s3, s4)
.filter(s -> s != null)
.collect(Collectors.joining());
給
abc
有幾點需要注意:
Stream<String>
。 String s = null;
System.out.println("s: " + (s != null ? s : ""));
但你真正要求的是一個空的合並運算符 。
您可以使用Apache Commons StringUtils.defaultString(String)
方法,或者如果您沒有使用任何第三方庫,您甚至可以編寫自己的方法,只需一個班輪。
private static String nullToEmptyString(String str) {
return str == null ? "" : str;
}
然后在你的sysout中使用它:
System.out.println("s: " + nullToEmptyString(s));
您可以聲明自己的concatenation
方法:
public static String concat(String... s)
{//Use varArgs to pass any number of arguments
if (s!=null) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<s.length; i++) {
sb.append(s[i] == null ? "" : s[i]);
}
return sb.toString();
}
else {
return "";
}
}
另一種方法是使用Java 7中添加的java.util.Objects.toString()
:
String s = null;
System.out.println("s: " + Objects.toString(s, ""));
這適用於任何對象,而不僅僅是字符串,當然,您可以提供另一個默認值,而不僅僅是空字符串。
試試這個
String s = s == null ? "" : s;
System.out.println("s: " + s);
當沒有任何對象分配給變量時,字符串變量(此處為s
)稱為null
。 所以使用空字符串""
初始化變量; 然后你可以連接字符串。
如果您的s變量可能為null或不為null,請在進一步使用之前使用條件運算符。 然后變量s
不再為null。
這是一個示例代碼:
String s1 = "Some Not NULL Text 1 ";
String s2 = null;
String s3 = "Some Not NULL Text 2 ";
s1 = s1 == null ? "" : s1;
s2 = s2 == null ? "" : s2;
s3 = s3 == null ? "" : s3;
System.out.println("s: " + s1 + s2 + s3);
樣本輸出:
s: Some Not NULL Text 1 Some Not NULL Text 2
我想到了兩種方法,第一種方法不是使用連接而是最安全的方法:
public static void substituteNullAndPrint(String format, String nullString, Object... params){
String[] stringParams = new String[params.length()];
for(int i=0; i<params.length(); i++){
if(params[i] == null){
stringParams[i] = nullString;
}else{
stringParams[i] = params[i].toSTring();
}
}
String formattedString = String.format(format, stringParams);
System.out.println(formattedString);
}
USAGE:
substituteNullAndPrint("s: %s,%s", "", s1, s2);
第二個使用連接,但要求你的實際字符串永遠不會包含“null”,所以它只能用於非常簡單的情況(無論如何,即使你的字符串永遠不是“null”我永遠不會使用它):
public static void substituteNullAndPrint(String str){
str.replace("null", "");
System.out.println(str);
}
USAGE:
substituteNullAndPrint("s: " + s1);
筆記:
- 因為我反對內聯很多條件,所以使用外部實用方法。
- 這沒有經過測試,我可能會將c#語法與Java混淆
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.