How to retrieve specific parts/values/os from a string?

Asked

Viewed 1,113 times

6

I have a ArrayList where I assemble a custom list that is displayed on 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 return below (in the System.out):

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

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

How can I "dismantle" that string and take only the number 405?

2 answers

5


How do you need to pick up only the number after codcal a way is by using regular expressions.

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

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

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));
}
  • Very good... Thank you very much Bruno!

  • Dispo @Fábioazevedo. If it has been helpful, consider accepting the answer :)

2

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

If the number you want to pick is always after codcal= and for the last part of your string, you can do so:

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 the substring() passing only one value you are setting the beginning of it in the String texto and taking everything that goes to its end.

If for example you have more fields in your String and they are separated by a comma, 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 took the index of the first comma after the beginning of the string, if you were using another character to separate its values it would be the case to replace in the line that initializes the variable fim.

For both cases, exit:

405

  • 1

    Thank you so much Math... gave it right too! Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.