-2

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.

Nivas Dhina
  • 149
  • 1
  • 10

5 Answers5

2
var str = "www.medicoshere.com/register.html?23457cedlske234cd";
var res = str.split("?");
var value = res.slice(-1).pop(); // it will give 23457cedlske234cd
Vineet
  • 4,525
  • 3
  • 23
  • 42
2

Do this:

var newString = 'www.medicoshere.com/register.html?23457cedlske234cd'.split('?')[1]
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
0

Use .split to go from strings to an array of the split substrings:

var s = "one, two, three, four, five"
s.split(", ");  // ["one", "two", "three", "four", "five"]

You can then access the individual parts with s[0] etc.

Graham
  • 7,431
  • 18
  • 59
  • 84
kenda
  • 488
  • 6
  • 16
0

Try:-

$scope.string = "www.medicoshere.com/register.html?23457cedlske234cd";
$scope.s = $scope.string.split("?")

console.log(s[1] );
ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41
0

You can get the part of the string you want in different ways

Using indexOf and substring

var str = "www.medicoshere.com/register.html?23457cedlske234cd";
var partIndex = str.indexOf("?");
var part = str.substring(partIndex + 1); // 23457cedlske234cd

Using Split

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.

Bond
  • 184
  • 2
  • 9