String Manipulations
We really like the @-fomula language's ability to pull out part of a string based on another string. For example, @Right("Hello", "e") returns "llo". In JavaScript, you have to use offsets. Here are a few JavaScript functions to mimic the @-function capabilities.
function rightString(fullString, subString) {
if (fullString.indexOf(subString) == -1) {
return "";
} else {
return (fullString.substring(fullString.indexOf(subString)+subString.length, fullString.length));
}
}
function rightBackString(fullString, subString) {
if (fullString.lastIndexOf(subString) == -1) {
return "";
} else {
return fullString.substring(fullString.lastIndexOf(subString)+1, fullString.length);
}
}
function middleString(fullString, startString, endString) {
if (fullString.indexOf(startString) == -1) {
return "";
} else {
var sub = fullString.substring(fullString.indexOf(startString)+startString.length, fullString.length);
if (sub.indexOf(endString) == -1) {
return sub;
} else {
return (sub.substring(0, sub.indexOf(endString)));
}
}
}
function middleBackString(fullString, startString, endString) {
if (fullString.lastIndexOf(startString) == -1) {
return "";
} else {
var sub = fullString.substring(0, fullString.lastIndexOf(startString));
if (sub.indexOf(endString) == -1) {
return sub;
} else {
return (sub.substring(sub.indexOf(endString)+endString.length, sub.length));
}
}
}
function leftString(fullString, subString) {
if (fullString.indexOf(subString) == -1) {
return "";
} else {
return (fullString.substring(0, fullString.indexOf(subString)));
}
}
function leftBackString(fullString, subString) {
if (fullString.lastIndexOf(subString) == -1) {
return "";
} else {
return fullString.substring(0, fullString.lastIndexOf(subString));
}
}