一个符号截取:
let str = 'hello, how are you doing? username!'
let before1 = str.substring(0,str.lastIndexOf("?"))
console.log(before1,'before1---------------');
let before2 = str.substring(0,str.lastIndexOf("?") + 1)
console.log(before2,'before2---------------');
let after1 = str.substring(str.lastIndexOf("?") + 1)
console.log(after1,'after1-----------------');
let after2 = str.substring(str.lastIndexOf("?"))
console.log(after2,'after2-----------------');
多个相同符号截取:
let str = 'hello? how are you doing? username!'
let index1 = str.indexOf("?")
let string1 = str.substring(0, index1);
console.log(string1,'string1------')
let string11 = str.substring(0, index1 + 1);
console.log(string11,'string11------')
let index2 = str.indexOf("?")
let string2 = str.substring(index2 + 1, str.length);
console.log(string2,'string2------')
let string22 = str.substring(index2, str.length);
console.log(string22,'string22------')
let index3 = str.lastIndexOf("?")
let string3 = str.substring(0, index3);
console.log(string3,'string3---------')
let string33 = str.substring(0, index3 + 1);
console.log(string33,'string33---------')
let index4 = str.lastIndexOf("?")
let string4 = str.substring(index4 + 1, str.length);
console.log(string4,'string4----------')
let string44 = str.substring(index4, str.length);
console.log(string44,'string44----------')