Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String[] to String and vice versa in Android

I want to convert a String Array to String so that later on while retrieving I can parse String to String[] with the help of (,) separator.

String [] ------------> String
//and later
String ---------------> String[]

Can someone guide on how to do this?

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

Log.d(TAG, "arrayData == " +arrayData[i]);

    // Joining:
    String joined = String.join(",", arrayData);
    //This will give error "The method join(String, String[]) is undefined for the type String"
}
like image 409
Christine Avatar asked Jan 08 '23 14:01

Christine


1 Answers

You can use String.join StringBuilder and String.split:

// Joining:
String joined = String.join(",", stringArr);
StringBuilder buffer = new StringBuilder();
for (String each : stringArr)
  buffer.append(",").append(each);
String joined = buffer.deleteCharAt(0).toString();

// Splitting:
String[] splitted = joined.split(",");
like image 86
Unihedron Avatar answered Jan 16 '23 10:01

Unihedron