Q:

Android substring

What is an efficient way to get number 17 from the following path?
/contacts/lookup/1217x31-2BN8629/17/data

It works with 
substring(path.lastIndexOf('/') - 2, path.lastIndexOf('/'))

But then it is hardcoded for a number 10-99, and if a number is for example 3, it will return /3 etc.

A:

The easiest, IMHO, would be to split the string according to the / character, and take the before-last element:
String[] parts = str.split("/");
String result = parts[parts.length - 2];

