Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use split?

I need to break apart a string that always looks like this:

something -- something_else.

I need to put "something_else" in another input field. Currently, this string example is being added to an HTML table row on the fly like this:

tRow.append($('<td>').text($('[id$=txtEntry2]').val())); 

I figure "split" is the way to go, but there is very little documentation that I can find.

like image 268
Matt Avatar asked Mar 31 '10 19:03

Matt


People also ask

What is the use of split?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How does split () work in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

How does split work in Java?

Java split() function is used to splitting the string into the string array based on the regular expression or the given delimiter. The resultant object is an array contains the split strings. In the resultant returned array, we can pass the limit to the number of elements.


1 Answers

Documentation can be found e.g. at MDN. Note that .split() is not a jQuery method, but a native string method.

If you use .split() on a string, then you get an array back with the substrings:

var str = 'something -- something_else'; var substr = str.split(' -- '); // substr[0] contains "something" // substr[1] contains "something_else" 

If this value is in some field you could also do:

tRow.append($('<td>').text($('[id$=txtEntry2]').val().split(' -- ')[0]))); 

like image 76
Felix Kling Avatar answered Sep 22 '22 11:09

Felix Kling