let split_whitespace = function(str) {
let searchTerm = " ",
indices = [],
out = [],
index = str.indexOf(searchTerm),
v = 0,
i = 0;
while (index !== -1) {
indices.push(index);
index = str.indexOf(searchTerm, index + 1);
}
for (var j = 0; j < indices.length; j++) {
if (indices.length > 0) {
out[i] = str.slice(v,indices[j]);
v = indices[i];
i++;
} else {
return -08;
}
}
return out;
}
const abs = "Hello XuWenxing ~";
const target = split_whitespace(abs);
console.log(target);
split_whitespace 方法返回一个由空格分隔 text 值子 slice 的迭代器。
我用javascript来实现了一下,不过不是迭代器