admin管理员组文章数量:1134590
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.
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.
Share Improve this question edited Jun 10, 2012 at 12:38 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Mar 31, 2010 at 19:16 MattMatt 5,66014 gold badges49 silver badges59 bronze badges 6 | Show 1 more comment4 Answers
Reset to default 252Documentation 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 it is the basic JavaScript split function, look at documentation, JavaScript split() Method.
Basically, you just do this:
var array = myString.split(' -- ')
Then your two values are stored in the array - you can get the values like this:
var firstValue = array[0];
var secondValue = array[1];
Look in JavaScript split() Method
- Mozilla Developer Network
- W3Schools
Usage:
"something -- something_else".split(" -- ")
According to MDN, the
split()
method divides a String into an ordered set of substrings, puts these substrings into an array, and returns the array.
本文标签:
javascriptHow to use splitStack Overflow
版权声明:本文标题:javascript - How to use split? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1736829686a1954645.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
版权声明:本文标题:javascript - How to use split? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736829686a1954645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
split()
problem. Give us more information :) – Felix Kling Commented Mar 31, 2010 at 19:26