字符串加入分隔符不适用于Android

时间:2015-04-11 09:03:12

标签: java android

我正在尝试将字符串数组转换为字符串并使用分隔符(,)连接所有值,但它只添加前两个值,转换代码中的错误。请参阅下面的内联评论

String[] array = new String[20];

for (int i = 0; i <= count; i++) {

   Log.d(TAG, "arrayvalue : " + array[i]); //Here I will get 5 values which is exact value count, but in next converted log I will have concatenation of only first two values, what is wrong in conversion code. 

   // Joining:
   StringBuilder buffer = new StringBuilder();
   for (String each : array)
        buffer.append(",").append(each);

   String joined = buffer.deleteCharAt(0).toString();

   Log.d("Prefs", "Converted Array to String : " + joined);

}

2 个答案:

答案 0 :(得分:2)

Android SDK中定义了String加入的标准方法:

final String joined = TextUtils.join(",", array);

答案 1 :(得分:0)

在动态数组中使用

String[] partno = new String[part.size()];
    for(int i = 0; i < part.size(); i++){
        partno[i]=part.get(i).getText().toString();
        Hpartno= TextUtils.join(",",partno);

    }