The expression str.substring(0, str.indexOf("begin:")).trim()
gets me the string before begin: but if I want the string that comes after begin:, what do I do?

- 6,156
- 15
- 56
- 98
5 Answers
Or use the same substring function but use the length as the first parameter (factoring in the length of "begin:") and nothing for the second:
str.substring(str.indexOf("begin:")+6).trim()
As the docs for substring state: "If indexEnd is omitted, substring() extracts characters to the end of the string."

- 204,283
- 31
- 260
- 272
-
HA! You see the silly answers thus far. I've had bad luck lately either getting positive upvotes but not the selected answer because I give the answer. I go and explain to them what it is they are doing wrong lol. whatever though I gave you the upvote – EasyBB Mar 25 '16 at 03:00
just split the string at begin: and get the next portion:
var strPortion=str.split("begin:");
var desiredString=strPortion[1].trim();
so if the string is: "Now we begin: it is better to try splitting the string";
the above code will give "it is better to try splitting the string";

- 15,194
- 2
- 25
- 27
-
**begin:** may come at the beginning of the string. Does your solution cover that scenario as well? – Sandah Aung Mar 25 '16 at 02:56
-
-
1
-
Also **trimLeft** and **trimRight** are available **http://stackoverflow.com/questions/498970/trim-string-in-javascript** – Arif Burhan Mar 25 '16 at 03:00
-
@EasyBB - I agree perhaps better to use substring - only reservation I have is that in the future if the phrase is changed it is a little more hassle to count the length of the string, but i concur. – gavgrif Mar 25 '16 at 03:02
-
@EasyBB - I guess the best reason to use this is that it allows the use of the text before the phrase - ie if there was a need to get the content preceding "begin: which would of course be strPortion[0], but this is not achievable in your solution. – gavgrif Mar 25 '16 at 03:05
-
-
1@EasyBB -oooopppps- i just looked at your fiddle and this is the result i saw: "begin: This is a story" - great solution.... at least my way works.... just saying! :)) – gavgrif Mar 25 '16 at 03:11
-
yes that is the before. the after works just comment out the last three lines or so – EasyBB Mar 25 '16 at 03:14
-
@EasyBB - except that "begin: This is a story" is not actually the text BEFORE the begin: but enough sparring - i prefer your option. I was just saying that mine works as well. onya BB – gavgrif Mar 25 '16 at 03:15
Not completely sure, but try:
str.substring(str.indexOf("begin:"),str.length).trim()
I'm not sure if you want 'begin' included or discarded.

- 507
- 4
- 12
-
you don't need the second parameter. if omitted it automatically goes to the end of the string. Also what you have will get the entire string. You need to do indexOf + the length of the string you're searching for. – EasyBB Mar 25 '16 at 02:58
var needle = "begin:";
var haystack = "begin: This is a story all about how my life got flipped turned upside down";
var phrase = str.substring(str.indexOf(needle) + needle.length).trim();
That will give you what you are looking for. I only am posting the answer due to others comments of the ambiguity and modulation of the code. So yeah.
Check it out and change the needle if you'd like. Code example shows how to get before the needle as well. https://jsfiddle.net/jL6zxcu3/1/

- 6,176
- 9
- 47
- 77
var needle = "begin:"
str.substring(str.indexOf(needle)+ needle.length, str.length).trim()
Or
var needle = "begin:"
str.substring(str.indexOf(needle)+ needle.length).trim()
The second Paramter is not needed if you want search to the end of string

- 151
- 1
- 8
-
Downvote for 2 reasons. One giving the answer, without no explanation. two copying what others already have. three creating a string to use the length property when if you have the string you should know the length. 4 not understanding the function at all. Omitting the second param does exactly what you have. – EasyBB Mar 25 '16 at 03:02
-
I am sorry for mistakes. but I have to explain that when I post it where is no any answers... – Kuawiiii Mar 25 '16 at 03:03
-
No, not close. I am sorry you think that. It's other minor discrepancies that I have. – EasyBB Mar 25 '16 at 04:28