RadicalActi
RadicalActi

Reputation: 191

How to Split many Strings with Jquery

Let's say I have a bunch of strings which I get from user input. Each strings starts with a new line like the following example:

gordon:davis
harley:vona
empir:domen
furno:shyko

I would like to write a function in jQuery which loads the user data from the form and splits each row's sting. However I would ONLY need the first part of the strings, like:

gordon
harley
empir
furno

Is there any simple solution for this?

I have found something called: $.each(split) but I didn't get it work.

I'm sorry I'm a really newbie in Jquery, I hope you guys can help me out! Thanks in advance!

Upvotes: 0

Views: 71

Answers (1)

Julian Descottes
Julian Descottes

Reputation: 486

Use String.prototype.split method.

"gordon:davis".split(":") will return ["gordon","davis"]

Based on this, using Array.prototype.map or a JQuery version :

var strings = ["gordon:davis", "harley:vona"];
var splitStrings = strings.map(function (string) {
    return string.split(":")[0];
});

Upvotes: 4

Related Questions