How to capture the last 5 characters of a URI

Asked

Viewed 46 times

0

Hello guys I would like to know how I can capture the last 5 characters of a link, for example in this link below I would like to capture the characters ". m3u8"

https:painel.iptvmove.com:25461/live/teste/1234/1224.m3u8

1 answer

1


Just use the method substring.

String.substring(tamanho da string - 5);

Ex:

String uri = "https://painel.iptvmove.com:25461/live/teste/1234/1224.m3u8";

System.out.print( uri.substring(uri.length() - 5) );

Demonstration

As well remembered by @Ronaldo Peres, always validate values (whether with the help of libraries, regex etc).

String uri = "https://painel.iptvmove.com:25461/live/teste/1234/1224.m3u8";

/* Validação com a classe URL */
try {
    new URL(uri);

    System.out.print( uri.substring(uri.length() - 5) );
} catch (MalformedURLException e) {
    System.out.println( "URL Inválida" );
}

/* Validação com a classe URLUtil */
if (URLUtil.isNetworkUrl(uri)) {
    System.out.print( uri.substring(uri.length() - 5) );
}

/* Verificação do tamanho da URL */
if (uri.length >= 5) {
    System.out.print( uri.substring(uri.length() - 5) );
}
  • Also check that the string size is greater than 5

  • Thank you very much!

Browser other questions tagged

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