Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab substring before a specified character jQuery or JavaScript

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, '.'));  
like image 992
Anjana Sharma Avatar asked Feb 03 '12 17:02

Anjana Sharma


People also ask

How do you get a substring that comes before a certain character?

Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.

How do you grab substring after a specified character jquery or JavaScript?

Alternatively, you can use the str. split() method. To get the substring after a specific character: Use the split() method to split the string on the character.

How can I get part of a string in JavaScript?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

Is substr () and substring () are same?

The difference between substring() and substr()The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .


2 Answers

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.

like image 200
wheresrhys Avatar answered Sep 29 '22 16:09

wheresrhys


var streetaddress = addy.split(',')[0]; 
like image 29
user3336882 Avatar answered Sep 29 '22 16:09

user3336882