What is the best way to get a part of a string from a given string using javascript
I have a line in JavaScript:
var str = "1:A;2:B;3:A;4:c;5:D";
How to choose an option before 2
, that is B
.
I am currently getting this use for a loop, breaking the string each ;
, but I want to know if there is a better way to achieve this without using the loop concept.
If you want to get an option before 2, you can use either:
- loop (my preferred)
-
indexOf
JavaScript:
function getAnswer(str) {
var position = str.indexOf(';2:')
// not found in string, or '2' is the first answer
if(position === -1) {
return;
}
return str.substring(position - 1, position)
}
RegExp
var num = '2';
"1:A;2:B;3:A;4:c;5:D".match((new RegExp(num+"\\:([\\S\\s]+?)\\;", "")))[1];
Fiddle
This uses RegExp (regular expressions). What this means, it will look for "2" in the string, then get the parameter associated with it
substring
I couldn't think of a better word, so forgive me. You can use a substring and .indexOf()
var num = '2',
string = "1:A;2:B;3:A;4:c;5:D",
index = string.indexOf(num+':');
string.substring(index+num.length+1,index+num.length+2);
Fiddle
Similar
The substring answer is a little easier to understand, but the following does the same
var num = '2',
string = "1:A;2:B;3:A;4:c;5:D";
string[string.indexOf(num+':')+num.length+1];
Fiddle
Protected
This should work in most situations. This will also get an option if it contains more than one letter
var string = "1:A;2:B;3:A;4:c;5:D",
num = '2',
result;
if (string.indexOf(';'+num+':') < 0) {
result = string.match(new RegExp(num+"\\:([\\S\\s]+?)\\;", ""))[1];
} else {
result = string.match((new RegExp('\\;'+num+"\\:([\\S\\s]+?)\\;", "")))[1];
}
In short:
var string = "1:A;2:B;3:A;4:c;5:D", num = '2', result = string.indexOf(";"+num+":") < 0? string.match(new RegExp(num+"\\:([\\S\\s]+?)\\;",""))[1] : string.match(new RegExp("\\;"+num+"\\:([\\S\\s]+?)\\;",""))[1];
alert(result);
Fiddle (I made it in one layer)
You can use split function like below:
var str = "1:A;2:B;3:A;4:c;5:D";
var result = str.split(":");
document.getElementById("Location").innerHTML = res[2];
If you know this is going to be a template, you can use something like:
var str = "1:A;2:B;3:A;4:c;5:D";
var i = 2;
console.log(str[str.indexOf(i)+2]);
//Output "B"
Maybe a recursive approach will help you.
var str = "1:A;2:B;3:A;4:c;5:D",
arr = str.split(";"),
len = arr.length;
function getVal(len){
if(len !== 0){
getVal(len-1);
if(arr[len-1].indexOf(2) === 0){
console.log(arr[len-1].split(":")[1])
};
};
};
getVal(len);
Nothing wrong with a loop, and ultimately, even if your code doesn't work, something on the computer will have to iterate over the string to do so. Anyway, if you don't want to loop over your code, you can do:
var str, new_str, start_position, end_position, final_result;
str = "1:A;2:B;3:A;4:c;5:D"; // or another similar string
new_str = ";" + str + ";" // add at the beginning and end, in case the string starts or ends with 2:SOMETHING, we still want to find it with indexOf below
start_position = new_str.indexOf(";2:");
if (start_position > -1) { // we found a 2
start_position = start_position + 3; // move past the ";2:"
end_position = new_str.indexOf(";", start_position); // first ";" after the start_position
final_result = new_str.substring(start_position, end_position);
}
Again, this is still a loop because it indexOf()
uses an inner loop. There is nothing wrong with a loop, almost every program written has at least one loop.