-1

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?

1
  • 1
    What if "Phone" does not occur at all inside the input? Commented Nov 21, 2013 at 22:10

5 Answers 5

1

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 !

Sign up to request clarification or add additional context in comments.

Comments

1

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();

Comments

0

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.

2 Comments

It's specified in the question "But in fact, I am only interested of all the characters up until the word Phone". I'm not making an assumption. @NickG
@NickG: +1 for humility :)
0

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!

Comments

-1
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 -

6 Comments

This would only work on this particular sentence. And wouldn't be a general case solution.
Wel probably he will not operate on the same string all the time, if that was the case the OP would just delete part of it manually
This is not a good way because it implies that the string will always be the same lenght, exemple if the adresse is 70, it will return 70 West Tasman Drive San Jose, CA 95134 United States - P
Love your opinions, but does it not answer the question? :P I didn't see the part where the OP stipulated it had to work for other strings. Otherwise, how do you know that they're not working with fixed length strings? It's not specified in the question! @MatthewGraves & Shryme
Yeah, but this relies on a person counting characters of a string. Even if it is for fixed length strings, this still requires a person to sit there and count up to 56. This solution simply doesn't use the tools that are available. @NickG
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.