简体   繁体   中英

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"
}

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(",");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM