Home > Net >  How to Remove Brackets from the Kotlin Array List
How to Remove Brackets from the Kotlin Array List

Time:06-08

I have a method like this. I want to remove the brackets of the array list when I take the output.

fun getAddOnNamesList(): ArrayList<String>? {
  if (!addOns.isNullOrEmpty()) {
    val addOnName = ArrayList<String>()
    addOnName.clear()
    for (addOnCat in addOns!!) {
      if (addOnCat.addon.mIsSelected)
        addOnName.add(addOnCat.addon.name   "\n")
    }
    Log.e("TAG", "getAddOnsList: $addOnName")
    return addOnName
  }
  return null
}

CodePudding user response:

fun getAddOnNamesList(): ArrayList<String>? {
  if (addOns.isNullOrEmpty()) {
    return null
  } else {
    val addOnNames = addOns.filter { it.addon.mIsSelected }.map { it.addon.name } as ArrayList
    val addOnNamesString = addOnNames.joinToString(",")
    Log.e("TAG", "getAddOnsList: $addOnNamesString")
    return addOnNames
  }
}

CodePudding user response:

You can output desired ArrayList without brackets by using joinToString() method, e.g.:

val list = arrayListOf("Text1", "Text2")
val string = list.joinToString(separator = ",")
  • Related