简体   繁体   English

如何在jQuery中找到字符串的子字符串?

[英]how to find substring of string in jQuery?

I can you please tell me how to split string to get out put. 我可以告诉我如何分割字符串以输出。

  • If I have this string: 'tc_1' Output is : first String : 'tc_' second string: '1' 如果我有以下字符串: 'tc_1'输出为:第一个字符串: 'tc_'第二个字符串: '1'
  • If I have this string: 'tc_1_tc_1' Output is : first String : 'tc_1_tc_' second string: '1' 如果我有以下字符串: 'tc_1_tc_1'输出为:第一个字符串: 'tc_1_tc_'第二个字符串: '1'
  • If I have this string: 'tc_1_tc_1_tc_2' Output is : first String : 'tc_1_tc_1_' second string: '2' 如果我有以下字符串: 'tc_1_tc_1_tc_2'输出为:第一个字符串: 'tc_1_tc_1_'第二个字符串: '2'

in jQuery ? 在jQuery中?

I use this 我用这个

var index = id.indexOf("_");
var count = id.substring(index + 1, id.length)

It works for the first case but does not work in other cases. 它适用于第一种情况,但不适用于其他情况。

You can use String.prototype.lastIndexOf() : 您可以使用String.prototype.lastIndexOf()

var index = id.lastIndexOf("_");
var count = id.substring(index + 1);

Also, you don't need the second argument id.length to substring() . 另外,您不需要substring()的第二个参数id.length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM