Javascript 在第 n 个字符出现时切割字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5494691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cutting a string at nth occurrence of a character
提问by Anonymous
What I want to do is take a string such as this.those.that
and get a substring to or from the nth occurrence of a character. So, from the start of the string to the 2nd occurrence of .
would return this.those
. Likewise, from the 2nd occurrence of .
to the end of the string would return that
. Sorry if my question is foggy, it's not that easy to explain. Also, please do not suggest making extra variables, and the result will be in a string and not an array.
我想要做的是获取一个字符串,例如this.those.that
并从第 n 个字符出现或从第 n 个字符中获取子字符串。因此,从字符串的开始到第二次出现.
将返回this.those
. 同样,从 的第二次出现.
到字符串的末尾将返回that
。对不起,如果我的问题是模糊的,那么解释起来并不容易。另外,请不要建议创建额外的变量,结果将是字符串而不是数组。
回答by alex
You could do it without arrays, but it would take more code and be less readable.
您可以在没有数组的情况下执行此操作,但需要更多代码且可读性较差。
Generally, you only want to use as much code to get the job done, and this also increases readability. If you find this task is becoming a performance issue (benchmark it), thenyou can decide to start refactoring for performance.
通常,您只想使用尽可能多的代码来完成工作,这也增加了可读性。如果您发现此任务正在成为一个性能问题(对其进行基准测试),那么您可以决定开始重构以提高性能。
var str = 'this.those.that',
delimiter = '.',
start = 1,
tokens = str.split(delimiter).slice(start),
result = tokens.join(delimiter); // those.that
console.log(result)
// To get the substring BEFORE the nth occurence
var tokens2 = str.split(delimiter).slice(0, start),
result2 = tokens2.join(delimiter); // this
console.log(result2)
回答by user2165842
Try this :
尝试这个 :
"qwe.fs.xczv.xcv.xcv.x".replace(/([^\.]*\.){3}/, '');
"xcv.xcv.x"
"qwe.fs.xczv.xcv.xcv.x".replace(/([^\.]*\.){**nth**}/, '');
- where is nth is the amount of occurrence to remove.
"qwe.fs.xczv.xcv.xcv.x".replace(/([^\.]*\.){**nth**}/, '');
- 其中 nth 是要删除的出现次数。
回答by hooke
Just in case somebody needs both "this" and "those.that" in a way as alex described in his comment, here is a modified code:
以防万一有人以亚历克斯在他的评论中描述的方式同时需要“这个”和“那些.那个” ,这里有一个修改后的代码:
var str = 'this.those.that',
delimiter = '.',
start = 1,
tokens = str.split(delimiter),
result = [tokens.slice(0, start), tokens.slice(start)].map(function(item) {
return item.join(delimiter);
}); // [ 'this', 'those.that' ]
document.body.innerHTML = result;
回答by NT3RP
I'm perplexed as to why you want to do things purely with string functions, but I guess you could do something like the following:
我很困惑为什么你想纯粹用字符串函数做事,但我想你可以做如下事情:
//str - the string
//c - the character or string to search for
//n - which occurrence
//fromStart - if true, go from beginning to the occurrence; else go from the occurrence to the end of the string
var cut = function (str, c, n, fromStart) {
var strCopy = str.slice(); //make a copy of the string
var index;
while (n > 1) {
index = strCopy.indexOf(c)
strCopy = strCopy.substring(0, index)
n--;
}
if (fromStart) {
return str.substring(0, index);
} else {
return str.substring(index+1, str.length);
}
}
However, I'd strongly advocate for something like alex's much simpler code.
但是,我强烈主张使用类似 alex 的简单得多的代码。
回答by RobG
If you really want to stick to string methods, then:
如果你真的想坚持使用字符串方法,那么:
// Return a substring of s upto but not including
// the nth occurence of c
function getNth(s, c, n) {
var idx;
var i = 0;
var newS = '';
do {
idx = s.indexOf(c);
newS += s.substring(0, idx);
s = s.substring(idx+1);
} while (++i < n && (newS += c))
return newS;
}