如何使用 javascript 在字符第 9 次出现后获取文本?
How to get text after the 9th occurrence of a character using javascript?
我得到以下字符串:
NPA-Woodburn,OR,Woodburn,OR,97071,Bensenville,IL,60106,150.00,0.00,cash/certified funds,enclosed,operable,
我需要遍历一些行并对“,”之后的第 9 个文本求和,但我就是做不到。
我见过很多解决方案的组合,但 none 让我明白了。
谢谢。
您可以split the string by that character, remove all items in the array before the 9th item (with slice
), then join按字符返回数组。
const character = ",";
const str = "NPA-Woodburn,OR,Woodburn,OR,97071,Bensenville,IL,60106,150.00,0.00,cash/certified funds,,enclosed,operable,,,,,";
const res = str.split(character).slice(9).join(',')
console.log(res)
要隔离第 8 次出现,您可以使 slice
调用中的范围更具体:
const character = ",";
const str = "NPA-Woodburn,OR,Woodburn,OR,97071,Bensenville,IL,60106,150.00,0.00,cash/certified funds,,enclosed,operable,,,,,";
const res = str.split(character).slice(8,9)[0]
console.log(res)
我得到以下字符串: NPA-Woodburn,OR,Woodburn,OR,97071,Bensenville,IL,60106,150.00,0.00,cash/certified funds,enclosed,operable,
我需要遍历一些行并对“,”之后的第 9 个文本求和,但我就是做不到。 我见过很多解决方案的组合,但 none 让我明白了。
谢谢。
您可以split the string by that character, remove all items in the array before the 9th item (with slice
), then join按字符返回数组。
const character = ",";
const str = "NPA-Woodburn,OR,Woodburn,OR,97071,Bensenville,IL,60106,150.00,0.00,cash/certified funds,,enclosed,operable,,,,,";
const res = str.split(character).slice(9).join(',')
console.log(res)
要隔离第 8 次出现,您可以使 slice
调用中的范围更具体:
const character = ",";
const str = "NPA-Woodburn,OR,Woodburn,OR,97071,Bensenville,IL,60106,150.00,0.00,cash/certified funds,,enclosed,operable,,,,,";
const res = str.split(character).slice(8,9)[0]
console.log(res)