我有四個字符串需要追加。Java/Android:追加多個字符串,如果字符串爲空,則跳過
String food = extras.getString("food");
String file = extras.getString("file");
String parcel = extras.getString("parcel");
String others = extras.getString("others");
String itemList = new StringBuilder(food).append("\n")
.append(file).append("\n")
.append(parcel).append("\n")
.append(others).toString();
此代碼將prints
,如果讓我選擇Food
和File
。
Food
File
null
null
由於Parcel
和別人有沒有值(這是null
),如何使它將打印象下面這樣?
Food
File
我試過使用if else
但它會太長(14種可能性)。有沒有其他方法可以使其更短,更有效?
什麼是'extras'的類型 –
@PavneetSingh它是一個包 – August
您支持的最低API級別是多少?你是否能夠使用這樣的'String food = extras.getString(「food」,「」);' –