How to grab substring before a specified character jQuery or JavaScript

JavascriptJquerySubstringSubstr

Javascript Problem Overview


I am trying to extract everything before the ',' comma. How do I do this in JavaScript or jQuery? I tried this and not working..

1345 albany street, Bellevue WA 42344

I just want to grab the street address.

var streetaddress= substr(addy, 0, index(addy, '.')); 

Javascript Solutions


Solution 1 - Javascript

var streetaddress= addy.substr(0, addy.indexOf(',')); 

While it's not the best place for definitive information on what each method does (mozilla developer network is better for that) w3schools.com is good for introducing you to syntax.

Solution 2 - Javascript

var streetaddress = addy.split(',')[0];

Solution 3 - Javascript

try this:

streetaddress.substring(0, streetaddress.indexOf(','));

Solution 4 - Javascript

//split string into an array and grab the first item

var streetaddress = addy.split(',')[0];

Also, I'd recommend naming your variables with camel-case(streetAddress) for better readability.

Solution 5 - Javascript

If you like it short simply use a RegExp:

var streetAddress = /[^,]*/.exec(addy)[0];

Solution 6 - Javascript

almost the same thing as David G's answer but without the anonymous function, if you don't feel like including one.

s = s.substr(0, s.indexOf(',') === -1 ? s.length : s.indexOf(','));

in this case we make use of the fact that the second argument of substr is a length, and that we know our substring is starting at 0.

the top answer is not a generic solution because of the undesirable behavior if the string doesn't contain the character you are looking for.

if you want correct behavior in a generic case, use this method or David G's method, not the top answer

regex and split methods will also work, but may be somewhat slower / overkill for this specific problem.

Solution 7 - Javascript

You can also use shift().

var streetaddress = addy.split(',').shift();

According to MDN Web Docs:

> The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

Solution 8 - Javascript

var newString = string.substr(0,string.indexOf(','));

Solution 9 - Javascript

var streetaddress = addy.substr(0, addy.indexOf('.')); 

(You should read through a javascript tutorial, esp. the part about String functions)

Solution 10 - Javascript

If you want to return the original string untouched if it does not contain the search character then you can use an anonymous function (a closure):

var streetaddress=(function(s){var i=s.indexOf(',');
   return i==-1 ? s : s.substr(0,i);})(addy);

This can be made more generic:

var streetaddress=(function(s,c){var i=s.indexOf(c);
   return i==-1 ? s : s.substr(0,i);})(addy,',');

Solution 11 - Javascript

You could use regex as this will give you the string if it matches the requirements. The code would be something like:

const address = "1345 albany street, Bellevue WA 42344";
const regex = /[1-9][0-9]* [a-zA-Z]+ [a-zA-Z]+/;
const matchedResult = address.match(regex);

console.log(matchedResult[0]); // This will give you 1345 albany street.

So to break the code down. [1-9][0-9]* basically means the first number cannot be a zero and has to be a number between 1-9 and the next number can be any number from 0-9 and can occur zero or more times as sometimes the number is just one digit and then it matches a space. [a-zA-Z] basically matches all capital letters to small letters and has to occur one or more times and this is repeated.

Solution 12 - Javascript

If you are worried about catching the case where no comma is present, you could just do this:

var end = Math.min(addy.indexOf(","), addy.length)
var streetaddress = addy.substr(0, end);

Nobody said it had to go on one line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnjana SharmaView Question on Stackoverflow
Solution 1 - JavascriptwheresrhysView Answer on Stackoverflow
Solution 2 - Javascriptuser3336882View Answer on Stackoverflow
Solution 3 - JavascriptMikey GView Answer on Stackoverflow
Solution 4 - JavascriptMiles FlorenceView Answer on Stackoverflow
Solution 5 - JavascriptfluView Answer on Stackoverflow
Solution 6 - Javascriptm aView Answer on Stackoverflow
Solution 7 - JavascriptGrant MillerView Answer on Stackoverflow
Solution 8 - JavascriptGauravView Answer on Stackoverflow
Solution 9 - JavascriptMira WellerView Answer on Stackoverflow
Solution 10 - JavascriptDavid GView Answer on Stackoverflow
Solution 11 - JavascriptRamen_Lover912View Answer on Stackoverflow
Solution 12 - JavascriptmonkeyView Answer on Stackoverflow