0

I need to split a string after a phrase for example id the string is "Stay in London" I need to remove "Stay in" I know this must be simple but I am finding it hard to find an approach to do this.

4
  • There's a lot of possible ways to do this - to get the best advice, you should be clearer as to what your actual goal is. Commented Aug 21, 2014 at 13:05
  • You can try replace function - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 21, 2014 at 13:05
  • "Stay in London".split(" ").pop() Commented Aug 21, 2014 at 13:07
  • @DylanCorriveau: I'd say it's not quite a duplicate, with a fixed string. That question is about dynamic strings and the answers aren't the obvious way of handling this question. Commented Aug 21, 2014 at 13:08

2 Answers 2

2
var str = "Stay in London",

//Here's a few ways, but there are many more 
//and it all depends how dynamic it needs to be...
london = str.split(' ').pop(),
london = str.match(/\b(\w+)$/)[0],
london = str.slice(8),
london = str.replace('Stay in ', ''),

//and we could get silly... ;)
london = [].filter.call(str, function (letter, index) {
    return index >= 8;
}).join(''),

london = str.split(' ').reduce(function (r, token) {
    if (r.tokensToExclude.indexOf(token) == -1) r.finalString += token;
    return r;
}, { tokensToExclude: ['Stay', 'in', ' '], finalString: '' }).finalString;
Sign up to request clarification or add additional context in comments.

Comments

1

Use the replace function:

"Stay in London".replace("Stay in ", "");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.