Split String using comma as parameter [duplicate]

0

I'm trying to get the values from a ListView to send them to another screen by clicking on the item.

So I have the following code:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String sttr = parent.getItemAtPosition(position).toString();
        System.out.println(sttr);

    }

The return of the String is:

I/System.out﹕ {perref=May / 2015, tipcal=Cálculo Mensal, codcal=408}

How can I separate 3 comma-separated values into 3 strings?

Has anyone ever come across the situation?

Is there any other way to do this?

    
asked by anonymous 29.05.2015 / 14:18

4 answers

3

It's like @Onaiggac says here .. Using the Split function.

If you need to use the values that have been separated, you can create an array of strings for later access to them

String[] sol;
String sttr = parent.getItemAtPosition(position).toString();
sol = sttr.split(",");

Now if you want to call the 1st String after splitting:

... sol[0];

The 2nd:

... sol[1];

and so on

    
29.05.2015 / 15:02
1

You can use the Split method with regular expression

  String sttr = parent.getItemAtPosition(position).toString();
  for (String retval: sttr.split(",")){
     System.out.println(retval);
  }

The Split method will return an array of strings for each comma found.

    
29.05.2015 / 14:30
1

You can also use this format:

String[] separated = CurrentString.split(",");
separated[0];
separated[1];
    
29.05.2015 / 15:08
-1

In addition to @Oigigac's response, it's possible (as @Math returned in another question):

        int iniPerRef = retorno.toString().indexOf("perref=")+7;
        int fimPerRef = retorno.toString().indexOf(",", iniPerRef);
        System.out.println(retorno.toString().substring(iniPerRef, fimPerRef));

        int iniTipCal = retorno.toString().indexOf("tipcal=")+7;
        int fimTipCal = retorno.toString().indexOf(",", iniTipCal);
        System.out.println(retorno.toString().substring(iniTipCal, fimTipCal));

        int iniCodCal = retorno.toString().indexOf("codcal=")+7;
        int fimCodCal = retorno.toString().indexOf("}", iniCodCal);
        System.out.println(retorno.toString().substring(iniCodCal, fimCodCal));
    
29.05.2015 / 15:09