Help - Extract url number to pause it to integer

3

I have been trying to extract from this url the number in the section of the image file to be able to load them with glide in android studio

Example: link however, I do not know how to do it

I've done this until now:

public int getNumber() {
    String [] urlPartes = urlImagen.split ("/");

    return Integer.parseInt (urlPartes[urlPartes.length -1]) ;
}

which returns this error:

java.lang.NumberFormatException: For input string: "local4.JPG"

I know my error is trying to extract the number but I do not know how. Thanks for your attention.

    
asked by Wilberth Rosas 06.12.2018 в 01:31
source

1 answer

1

I have solved it in the following way:

public int getNumber(){
    String urlImagen = "https://t-organizagroup.com/ws_easyhotel/public/api/imagen/local/local4.JPG";
    String [] urlPartes = urlImagen.split ("/");
    String numeroStr = urlPartes[urlPartes.length -1];
    int numero = Integer.parseInt(numeroStr.replaceAll("\D+",""));
    return numero;
}

What I do is: assign a variable of type String the last part of the url, just as you do it (numberStr), then through the function replaceAll (), I use a regular expression that takes everything character "\ D +" and replace it with empty ""; with this you will have only the numerical part that finally parsed it to whole, which is what you are looking for.

    
answered by 06.12.2018 / 04:04
source