gpt4 book ai didi

android - 如果字符串不为 null 则想要最短的方法然后添加否则不

转载 作者:太空狗 更新时间:2023-10-29 16:42:50 28 4
gpt4 key购买 nike

如果这些不等于 null,我想添加街道、城市、邮政编码、国家/地区,如果街道为空,则不要添加街道,我有方法,但我想为此获得最短和最好的方法...

map.put(KEY_ADDRESS, street+" "+city +" " +zipcode+ " "+country);

我的方法是这样的

if(street.trim().length()>0&&city.trim().length()>0&&zipcode.trim().length()>0&&country.trim().length()>0)
{
map.put(KEY_ADDRESS, street+" "+city +" " +zipcode+ " "+country);
}
else if(){

}
else if(){

}

我想要最短的方法来检查所有字符串是否为空,然后不要将该字符串添加到映射中...提前致谢

最佳答案

按顺序将所有字符串存储在数组中:

String[] sa = new String{street, city, zipcode, country};

private String getAddress(String[] sa){

String s = "";
for(i = 0; i < 4; i++){

if(sa[i] != null){
s = s + " " + sa[i].trim();
}
}
return s;
}

关于android - 如果字符串不为 null 则想要最短的方法然后添加否则不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14952238/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com