Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: slicing a String

Tags:

java

I have URLs which always end on a number, for example:

  String url = "localhost:8080/myproject/reader/add/1/";
  String anotherurl = "localhost:8080/myproject/actor/take/154/";

I want to extract the number between the last two slashes ("/").

Does anyone know how I can do this?

like image 988
Alex Dowining Avatar asked Dec 13 '22 01:12

Alex Dowining


1 Answers

You could split the string:

String[] items = url.split("/");
String number = items[items.length-1]; //last item before the last slash
like image 110
assylias Avatar answered Dec 30 '22 22:12

assylias