简体   繁体   中英

How to remove a character of a string within an arraylist<String>?

I have an array list like this: [name1, name2,name3] When I put that in a spinner, it prints the name this way:

I need to remove the "[" in the first position of the array(so the first name) and the "]" in the last position of my array(so the last name).

that's my code:

//Extracting participants ArrayList from the document
                        for(Object item : task.getResult().getData().values()) {
                            String[] values = String.valueOf(item).split(",");

                           // values[1] = values[1].substring(1, values[1].length());
                            for (String value : values){

                                    partecipantsArrayList.add(value);

                            }

the line of code with the comment, doesn't work.

Follow below steps

  1. Remove [ from your string

     values = values.replace('[',""); 
  2. Remove ] from your string

     values = values.replace(']',""); 
  3. Then split the string with , .

//Extracting participants ArrayList from the document
    for (Object item : task.getResult().getData().values()) {
        String itemToString = item.toString();
        itemToString = itemToString.subSequence(1, itemToString.length() - 1).toString();
        String[] values = itemToString.split(",");
        for (String value : values) {
            partecipantsArrayList.add(value);
        }
    }

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