如何使用拆分? [英] How to use split?
问题描述
我需要拆分一个总是这样的字符串:
I need to break apart a string that always looks like this:
something-something_else。
something -- something_else.
我需要在另一个输入字段中输入something_else。目前,这个字符串示例正在被添加到HTML表格行中,如下所示:
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.
推荐答案
可以找到文档,例如在 MDN 。请注意 .split()
是不是 jQuery方法,而是本机字符串方法。
Documentation can be found e.g. at MDN. Note that .split()
is not a jQuery method, but a native string method.
如果对字符串使用 .split()
,那么你会得到一个带有子串的数组:
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])));
这篇关于如何使用拆分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!