Reputation: 13
I am looking to do some string manipulations in javascript
My strings look like this
170 West Tasman Drive San Jose, CA 95134 United States - Phone: 408-526-4000 Website:
But in fact, I am only interested of all the characters up until the word Phone (not including phone)
So, in this case, I would get:
`170 West Tasman Drive San Jose, CA 95134 United States -
I looked at this, http://www.javascriptkit.com/javatutors/string4.shtml, but I am not finding anything that I think would help me...
Any ideas, on how to pull this string in javascript?
Upvotes: -1
Views: 115
Reputation: 7820
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!
Upvotes: 0
Reputation: 36511
Assuming that the dash will always be present, you could use:
var str = '170 West Tasman Drive San Jose, CA 95134 United States - Phone: 408-526-4000 Website:';
str = str.split('-').shift();
You could actually split on 'Phone' just as easily though:
var str = '170 West Tasman Drive San Jose, CA 95134 United States - Phone: 408-526-4000 Website:';
str = str.split('Phone').shift();
Upvotes: 1
Reputation: 25854
console.log("170 West Tasman Drive San Jose, CA 95134 United States - Phone: 408-526-4000 Website:".substring(0,56))
gives
170 West Tasman Drive San Jose, CA 95134 United States -
Upvotes: -1
Reputation: 1570
You can use str.indexOf("Phone"); to get the index of the world Phone, then do a substring from the start to the position you just found, it will return you everything before phone !
Upvotes: 1
Reputation: 3284
This:
var string = '170 West Tasman Drive San Jose, CA 95134 United States - Phone: 408-526-4000 Website:';
string = string.substr(0,string.indexOf('Phone'));
alert(string);
-substr is short for substring and works like this substring(being, end)
-indexOf finds the first occurence of the string provided.
Upvotes: 0