删除没有分隔符的字符串中的重复字符串
Delete repeated string in a string with no delimiter
我正在使用来自另一个系统的一些无法更改的输出,这需要我在 while 循环中整理每次迭代的输出。
迭代 1 的输出:John Smith
迭代 2 的输出:John SmithJohn Smith
如何使用 Javascript 整理输出,使其成为:
迭代 1 的输出:John Smith
迭代 2 的输出:John Smith
当前代码:
var names = document.evaluate("/html/body/div[1]/c-wiz/div[1]/div/div[5]/div[3]/div[3]/div/div[2]/div[2]/div[2]/span[1]/div[2]/div/div/div", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var thisName = names.iterateNext();
var arr = [];
while (thisName) {
arr.push(nameComplete);
thisName = names.iterateNext();
}
console.log(arr);
输出:
["Josh ParkerJosh Parker", "John SmithJohn Smith", "David JamesDavid James"]
如果您的输入加倍,而您只需要一半的字符串,则使用 String.prototype.slice()
和字符串长度来获取前半部分。
var names = ["Josh ParkerJosh Parker", "John SmithJohn Smith", "David JamesDavid James"];
var arr = [];
for (var thisName of names) {
arr.push(thisName.slice(0, thisName.length/2));
}
console.log(arr);
我正在使用来自另一个系统的一些无法更改的输出,这需要我在 while 循环中整理每次迭代的输出。
迭代 1 的输出:John Smith
迭代 2 的输出:John SmithJohn Smith
如何使用 Javascript 整理输出,使其成为:
迭代 1 的输出:John Smith
迭代 2 的输出:John Smith
当前代码:
var names = document.evaluate("/html/body/div[1]/c-wiz/div[1]/div/div[5]/div[3]/div[3]/div/div[2]/div[2]/div[2]/span[1]/div[2]/div/div/div", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var thisName = names.iterateNext();
var arr = [];
while (thisName) {
arr.push(nameComplete);
thisName = names.iterateNext();
}
console.log(arr);
输出:
["Josh ParkerJosh Parker", "John SmithJohn Smith", "David JamesDavid James"]
如果您的输入加倍,而您只需要一半的字符串,则使用 String.prototype.slice()
和字符串长度来获取前半部分。
var names = ["Josh ParkerJosh Parker", "John SmithJohn Smith", "David JamesDavid James"];
var arr = [];
for (var thisName of names) {
arr.push(thisName.slice(0, thisName.length/2));
}
console.log(arr);