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.
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.
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.
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.
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])));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With