安卓分割空字符串数组长度为1

空值分割总有1个长度,困惑了很久。

Java split分割空字符串得到的数组长度为1_java数组length为1的情况-CSDN博客

计数

java 复制代码
int count_bookmark = 0;
SharedPreferences SP;

SP = PreferenceManager.getDefaultSharedPreferences(this);
String s = SP.getString("bookmarks", "");        
if (!s.isEmpty()) {
    String[] sa = s.split(":");
    count_bookmark = sa.length;
}

加载

java 复制代码
void loadBookmarks() {
    String s = sharedPreferences.getString("bookmarks", "");    
    String[] sa = s.split(":");
    Collections.addAll(list, sa);
    list.remove("");
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
    listView.setAdapter(adapter);
}

添加

java 复制代码
SharedPreferences SP;
SharedPreferences.Editor SPE;

String s = SP.getString("bookmarks", "");
if (s.isEmpty())
    SPE.putString("bookmarks", path);
else
    SPE.putString("bookmarks", ":" + path);
SPE.apply();
相关推荐
阿pin2 小时前
Android随笔-kotlin withContext
android·kotlin·withcontext
树码小子3 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
Android-Flutter4 小时前
android 动画详解
android·kotlin
小孔龙9 小时前
GraphicBuffer 跨进程共享
android
行业研究员10 小时前
Android内置GPS与腾讯地图定位技术对比分析
android·android定位·腾讯地图定位
Android-Flutter11 小时前
android WMS 详解(二)
android·kotlin
游戏开发爱好者811 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者812 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
阿pin13 小时前
Android随笔-kotlin 高阶函数
android·kotlin·高阶函数
wxson72821 天前
【Android视频监控系统技术总结】
android·音视频