简体   繁体   中英

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'
  • If I have this string: 'tc_1_tc_1' Output is : first String : 'tc_1_tc_' second string: '1'
  • If I have this string: 'tc_1_tc_1_tc_2' Output is : first String : 'tc_1_tc_1_' second string: '2'

in 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() :

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

Also, you don't need the second argument id.length to substring() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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