Q:

Remove after a particular String

I have some text content    
atxtCtnt = "Helow Hay How Are You"

if Hay exists I need to remove Hay How Are You
I did it like this: 
var atxtCtnt = "Helow Hay How Are You",
txt = atxtCtnt.substring(atxtCtnt.indexOf("Hay"));
atxtCtnt = atxtCtnt.replace(txt , "");
alert(atxtCtnt );

Please help me in better way without RegExp

A:

If you don't want to use Regex...
I think this will make your code a little bit shorter
a = atxtCtnt.indexOf('Hay');
atxtCtnt = a >=0?atxtCtnt.substring(0,a):atxtCtnt;

A:

You need the sub string from 0 to the index. But only if the index is more than 0 (The term "Hay" exists in the text). Like this
var atxtCtnt = "Helow Hay How Are You";
var index = atxtCtnt.indexOf("Hay");
var newText = (index < 0) ? atxtCtnt : atxtCtnt.substring(0, index);
alert(newText);

