1

We have a string ,

var str = "Name=XYZ;State=TX;Phone=9422323233";

Here in the above string we need to fetch only the State value i.e TX. That is 2 characters after the substring State=

Can anyone help me implement it in javascript.

0

5 Answers 5

1

.split() the string into array and then find the index of the array element having State string. Using that index get to that element and again .split() it and get the result. Try this way,

var str = "Name=XYZ;State=TX;Phone=9422323233";

var strArr = str.split(';');

var index = 0;

for(var i = 0; i < strArr.length; i++){
    if(strArr[i].match("State")){
        index = i;
    }
}

console.log(strArr[index].split('=')[1]);

jsFiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. However the State= position will be different sometimes. That is we may get the string as "Name=XYZ;Phone=9422323233;State=TX";
@Skyrim, Then you need to find the index of the State after using split() on it. And then get the value of State. Check the updated answer.
0

I guess the easiest way out is by slicing and splitting

var str = "Name=XYZ;State=TX;Phone=9422323233";
var findme = str.split(';')[1];
var last2 = findme.slice(-2);
alert(last2);

Need more help? Let me know

Comments

0

indexOf returns the position of the string in the other string.

Using this index you can find the next two characters

javascript something like

 var n = str.indexOf("State=");

then use slice method

like

var res = str.slice(n,n+2);

another method is :

use split function

 var newstring=str.split("State=");

then

var result=newstring.substr(0, 2);

Comments

0

Check this:

var str1 = "Name=XYZ;State=TX;Phone=9422323233";
var n = str1.search("State");
n=n+6;
var res = str1.substr(n, 2);

The result is in the variable res, no matter where State is in the original string.

2 Comments

what about this string var str = "Name=XYZ;Phone=9422323233;State=TXBBP";? It won't work with your code I guess.
It will bring out "TX" only, because is selecting only two characters. Is the state code of variable length?
0

There are any number of ways to get what you're after:

var str = "Name=XYZ;State=TX;Phone=9422323233"

Using match:

var match = str.match(/State=.{2}/);
var state = match? match[0].substring(6) : '';
console.log(state);

Using replace:

var state = str.replace(/^.*State=/,'').substring(0,2);
console.log(state);

Using split:

console.log(str.split('State=')[1].substring(0,2));

There are many other ways, including constructing an object that has name/value pairs:

var obj = {};
var b = str.split(';');
var c;

for (var i=b.length; i; ) {
  c = b[--i].split('=');
  obj[c[0]] = c[1];
}

console.log(obj.State);

Take your pick.

4 Comments

Nice answer, i am also give an answer to this question . but your answer is so nice, it shows how to approach a problem in different ways .
who knows if State will always have a value with length of 2? You last solution is your best one.
@AshadShanto—"That is 2 characters after the substring State=". The object method addresses that aspect too.
@AshadShanto you are right, but with a simple validation he can do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.