Uncias
Uncias

Reputation: 80

Loading data to a String Array from a String holding the array data in java Android Studio

I actually have a String that contains the format of String array. And I want to load that string data into the array.

String data = "{"Sam" , "14","USA","7th"}";

and the String Array

String loaded_data[][];

Thanks in advance...

Upvotes: 0

Views: 161

Answers (1)

Tony
Tony

Reputation: 466

I think this is what you wanted.

Using String.split() splits the string into an string array with , regex.

    public void something() {
        String data = "{'Sam' , '14','USA','7th'}";

        data = data.substring(1, data.length() - 1); //Ignores curly brackets
        data = data.replaceAll("'", ""); //Removes apostrophes
        data = data.replaceAll(" ", ""); //Removes whitespace


        String[] loadedData = data.split(","); //Splits string -> string[]

        System.out.println(Arrays.toString(loadedData));
    }

println:

[Sam, 14, USA, 7th]

Upvotes: 1

Related Questions