How to retrieve specific parts / values from a string?

6

I have ArrayList where I am adding a custom list that is displayed in a ListView . What I need is to pass the value of the selected item to another screen.

See below the method that will call the other screen:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Object obj = parent.getItemAtPosition(position);

    System.out.println(obj);

    Intent it = new Intent(this, ActHolerite.class);

    startActivityForResult(it, 0);
}

In the above method I have the following return (in System.out ):

  

I / System.out: {tipcal = Monthly Calculation, perref = April / 2015, codcal = 405}

What I need to pass to another screen is just the 405 (relative to codcal=405 ) because it is a key field of a select that I will use on this other screen.

How can I " disassemble " this string and just get the number 405 ?

    
asked by anonymous 18.05.2015 / 22:18

2 answers

5

As you need to get only the number after codcal one way is using regular expressions.

For example, we can use this pattern: codcal=(\d+) . That is, it will "match" the string where codcal= exists, followed by number, in any quantity.

Then, to recover, we can search for this pattern in the string and, if it exists, retrieve the group we are interested in, the group number demarcated.

An example would be this:

final String string = "I/System.out: {tipcal=Cálculo Mensal, perref=Abril / 2015, codcal=405}";
final Pattern pat = Pattern.compile("codcal=(\d+)");
final Matcher mat = pat.matcher(string);
if (mat.find()) {
    System.out.println(mat.group(1));
}
    
18.05.2015 / 22:32
2

You can use substring() to get only the part of the text that interests you.

If the number you want to get is always after codcal= and is the last part of your String, you can do this:

public class Str {
    public static void main(String[] args) {
        String texto = "tipcal=Cálculo Mensal, perref=Abril / 2015, codcal=405";
        int inicio = texto.indexOf("codcal=")+7;
        System.out.println(texto.substring(inicio));
    }
}

Using substring() passing only one value you are setting the beginning of it within the String texto and taking everything that goes to the end.

If for example you have more fields in your String and are separated by commas, you can do so:

public class Str {
    public static void main(String[] args) {
        String texto = "tipcal=Cálculo Mensal, perref=Abril / 2015, codcal=405, mais_algo_aqui=1111";
        int inicio = texto.indexOf("codcal=")+7;
        int fim = texto.indexOf(",", inicio);
        System.out.println(texto.substring(inicio, fim));
    }
}

In this case we pass two parameters, the beginning and the end of your substring. For the value fim we get the index of the first comma after the start of the String, if you were using another character to separate its values would be the case of replacing in the line that initializes the variable fim .

For both cases, output:

  

405

    
18.05.2015 / 23:18