One (different from the other answer) possibility would be to use Regular Expressions - this way, you are also flexible, should the text before the word Phone change in length. You need to:
The code that uses regular expressions can look like this:
var input = "170 West Tasman Drive San Jose, CA 95134 United States - Phone: 408-526-4000 Website:"; var regExPattern = /Phone:/; // the expression that is matched against var cutPosition = input.search(regExPattern); // find the start position of the word by searching a match if (cutPosition != -1) { var text = input.substring(0, input.search(regExPattern)); // do something with the text }
IMO using regular expressions in this case could make the task easier - there is also the option to remove the text (starting from Phone:) at once in one line using the string.replace method:
var input = "170 West Tasman Drive San Jose, CA 95134 United States - Phone: 408-526-4000 Website:"; var regExPattern = /Phone:.*/; // the expression that is matched against - now it is Phone: and everything after var text = input.replace(regExPattern, ""); // replace Phone: and everything after with empty string // do something with text
assumption: the text Phone: is always present!