Let say I have the following strings:
var Y = "TEST" var X = "abc 123 TEST 456 def"
I would like to get the string from X that comes after the word that specified in Y.
In this example it will be:
var Z = " 456 def"
Extract text before or after space with formula in Excel Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button.
To get text following a specific character, you use a slightly different approach: get the position of the character with either SEARCH or FIND, subtract that number from the total string length returned by the LEN function, and extract that many characters from the end of the string.
Probably the fastest way will be to use .slice
, .substr
or .substring
:
var Z = X.slice(X.indexOf(Y) + Y.length);
However there are some other alternatives, like with regular expressions:
var Z = X.replace(new RegExp('.*' + Y), '');
Or the one with arrays, proposed by @AustinBrunkhorst in the comments:
var Z = X.split(Y).pop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With