javascript - Retrieve a SubString from a String in the fastest possible way. Format from the end is fixed but not from the beginning, and Length is also not fixed -
i have string contains current location of person. want retrieve 2 strings appears before city name, string here
unnamed road, lal qila, chandni chowk, new delhi, city 110006, india. here want extract chandni chowk, new delhi. remember city name can of length. below code can u pls reduce time complexity
var c = 0; var arr = []; for(var = location.length - 15; >= 0; i--){ if (c > 2) { break; } if (location[i] == ",") { c++; arr.push(i); // arr[c] = i; } } if (c == 2) { final_location = location.substring(arr[1] + 2, arr[0]); } else if (c == 3){ final_location = location.substring(arr[2] + 2, arr[0]); } thanks lot guys of ur help
if formatting fixed end use way, this edit based on op's request
var split = 'unnamed road, lal qila, chandni chowk, new delhi, city 110006, india'.split(','); var area= split[split.length-5];// have chandni chowk var city= split[split.length-4];// have new delhi
Comments
Post a Comment