VIKAS KOHLI
VIKAS KOHLI

Reputation: 8440

How to get characters from second last index using indexof in javaScript?

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var n = absoluteURL.lastIndexOf('/');
var result = absoluteURL.substring(n + 1);
//alert(result);
console.log(result);

Here I get the result like 'vikas-kohli' as I am using lastIndexOf.

Now if someone wants to get characters from second last index, or it may be 3rd last index, then how can I get this?

I hope I am able to explain my question well

Upvotes: 5

Views: 8118

Answers (10)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27202

  • Insteadof using lastIndexOf('/') use string split('/') method.

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var splittedStr = absoluteURL.split('/');
console.log(splittedStr);

  • Then get the required element from an array.

    var res = splittedStr[splittedStr.length-n]; // n: 1,2,3.. cosnole.log(res);

DEMO

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var splittedStr = absoluteURL.split('/');
console.log(splittedStr[splittedStr.length-2]);

Upvotes: 6

Alexandre Nicolas
Alexandre Nicolas

Reputation: 1949

This should work:

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var results = absoluteURL.split('/');
var result = results[results.length-2];
alert(result);

Upvotes: 0

vatz88
vatz88

Reputation: 2452

Concatenate string from the index you want after splitting it with /

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli";     
var n = 2; // set the index from where you want to get characters
var arr = absoluteURL.split('/');
var a = [];
for(var i=n,j=0; i>=0; i--,j++){
  a[j] = arr[arr.length-i];
}
console.log(a.join('')); // you can join it with any character

Upvotes: 0

Caner Tezcan
Caner Tezcan

Reputation: 166

Hi if u can get nth Index from user u can use this

 var nthIndex = 2;
 var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"  
 for (i = 0; i < nthIndex; i++) {   

        var n = absoluteURL.lastIndexOf('/');           
            absoluteURL = absoluteURL.substring(0,n);}
alert(absoluteURL);

Upvotes: 0

Aabhas Arora
Aabhas Arora

Reputation: 23

We can add a function to do that ourselves.

Add below code snippet:

String.prototype.nIndexOf = function (char, index) {
if (index >= this.split(char).length)
    return -1;
else
    return this.split(char, index).join(char).length;
}

Then you can use this as following:

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var n1 = absoluteURL.nIndexOf('/', 4);
var n2 = absoluteURL.nIndexOf('/', 5);
var result = absoluteURL.substring(n1 + 1, n2);
alert(result);

Upvotes: 0

Jai
Jai

Reputation: 74738

You can use .split() on the window.location.pathname:

var locPath = "users/6262169/vikas-kohli"; // window.location.pathname;
var arr = locPath.split('/');

arr.forEach(function(item){
  console.log(item);
});

With specific indexes:

var locPath = "users/6262169/vikas-kohli"; // window.location.pathname;
var arr = locPath.split('/');

console.log('last::::',arr[arr.length-1]);
console.log('second last::::', arr[arr.length-2]);

Or subsequent pop() can give you last item of array:

var locPath = "users/6262169/vikas-kohli"; // window.location.pathname;
var arr = locPath.split('/');

console.log(JSON.stringify(arr, 0, 0), '<---last::::popped-->',arr.pop());
console.log(JSON.stringify(arr, 0, 0), '<-----second last::::popped--->', arr.pop());

Upvotes: 0

Hello Kitty
Hello Kitty

Reputation: 83

A simple for loop would do the trick.

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"    
var n = absoluteURL.indexOf('/');
absoluteURL = absoluteURL.substring(n+2); // Becasue there are two / first

for(i = 0; i < x ; i++) // x should be such that if it is 1, then only the last string will come, if 2, then the last two strings
{
    n = absoluteURL.indexOf('/');
    absoluteURL = absoluteURL.substring(n+1);
}

Upvotes: 0

Duong Tuan Anh
Duong Tuan Anh

Reputation: 11

IndexOf or something like that may not what you need. You can use split instead.

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli";
var partNo = 3;
var parts = absoluteURL.split('/');
alert(parts[parts.length - partNo]);

Upvotes: 0

Mistalis
Mistalis

Reputation: 18279

Just split your URL with /:

var absoluteUrl = "http://stackoverflow.com/users/6262169/vikas-kohli";window.location.pathname;
var splitedUrl = absoluteUrl .split('/');
console.log(splitedUrl);

Then get the element you want in the array.

Upvotes: 0

Rick Lee
Rick Lee

Reputation: 763

Sth like this?

var absoluteURL = "http://stackoverflow.com/users/6262169/vikas-kohli"     
var n = 2 //second last
var arr = absoluteURL.split('/')
console.log(arr[arr.length-n])

Upvotes: 0

Related Questions