java - What is the fastest way to find the last part of a url path? -


i have url such as:

"http:/www.someco.com/news/2016-01-03/waterloo-station"

the url never contains query string.

what cleanest way extract string "waterloo-station" ?

of course can use following code:

url.substring(url.lastindexof('/') + 1)) 

but not happy because has execute search last index , substring. wondering if there better way, (using regular expression?) same result in single step.

of course solution should faster when executed billions of times.

i not think can improved. short answer because search last index simple operation, can implemented fast algorithm (directly in string class!) , difficult regular expression fast this. second access string, can see, couldn't cost less: initialisation of new string.

it have been faster if there dedicated method implemented directly in string class.

if want more details, can see code in jdk. copied here convenience.

the following code implementation of method lastindexof() in jdk:

public int lastindexof(int ch, int fromindex) {     int min = offset;     char v[] = value;      int = offset + ((fromindex >= count) ? count - 1 : fromindex);      if (ch < character.min_supplementary_code_point) {         // handle cases here (ch bmp code point or         // negative value (invalid code point))         (; >= min ; i--) {             if (v[i] == ch) {                 return - offset;             }         }         return -1;     }      int max = offset + count;     if (ch <= character.max_code_point) {         // handle supplementary characters here         char[] surrogates = character.tochars(ch);         (; >= min; i--) {             if (v[i] == surrogates[0]) {                 if (i + 1 == max) {                     break;                 }                 if (v[i+1] == surrogates[1]) {                     return - offset;                 }             }         }     }     return -1; } 

being implemented directly in string class, has access private members:

/** value used character storage. */ private final char value[];  /** offset first index of storage used. */ private final int offset;  /** count number of characters in string. */ private final int count; 

it not working on substrings. in same time, substring method fast in java because not create new array of char, creates new string object changing offset , count:

public string substring(int beginindex, int endindex) {     if (beginindex < 0) {         throw new stringindexoutofboundsexception(beginindex);     }     if (endindex > count) {         throw new stringindexoutofboundsexception(endindex);     }     if (beginindex > endindex) {         throw new stringindexoutofboundsexception(endindex - beginindex);     }     return ((beginindex == 0) && (endindex == count)) ? :         new string(offset + beginindex, endindex - beginindex, value); }  // package private constructor shares value array speed. string(int offset, int count, char value[]) {     this.value = value;     this.offset = offset;     this.count = count; } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -