I wanted to split a string and get the string in two parts.
for example:
www.medicoshere.com/register.html?23457cedlske234cd
i wish to split the string in the above url and store the string that is after the ? to a variable. How can i do that.
I wanted to split a string and get the string in two parts.
for example:
www.medicoshere.com/register.html?23457cedlske234cd
i wish to split the string in the above url and store the string that is after the ? to a variable. How can i do that.
var str = "www.medicoshere.com/register.html?23457cedlske234cd";
var res = str.split("?");
var value = res.slice(-1).pop(); // it will give 23457cedlske234cd
Do this:
var newString = 'www.medicoshere.com/register.html?23457cedlske234cd'.split('?')[1]
Try:-
$scope.string = "www.medicoshere.com/register.html?23457cedlske234cd";
$scope.s = $scope.string.split("?")
console.log(s[1] );
You can get the part of the string you want in different ways
var str = "www.medicoshere.com/register.html?23457cedlske234cd";
var partIndex = str.indexOf("?");
var part = str.substring(partIndex + 1); // 23457cedlske234cd
var part = str.split("?")[1];
The only problem is that, if you use substring
, part will contain the whole string if the part you tried finding is not part of the string, say
var str = "www.medicoshere.com/register.html#23457cedlske234cd";
var partIndex = str.indexOf("?");
var part = str.substring(partIndex + 1); // www.medicoshere.com/register.html#23457cedlske234cd
while using split would return undefined
as the value of part.