简体   繁体   English

在第n次出现的字符处剪切一个字符串

[英]Cutting a string at nth occurrence of a character

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. 我想要做的是取一个像this.those.that这样的字符串,并从第n个字符出现一个子字符串。 So, from the start of the string to the 2nd occurrence of . 所以,从字符串的开头到第二次出现. would return this.those . 将返回this.those Likewise, from the 2nd occurrence of . 同样,从第2次出现. to the end of the string would return that . 到字符串的结尾将返回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. 另外,请不要建议制作额外的变量,结果将是字符串而不是数组。

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), then you 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) 

jsFiddle . jsFiddle

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. - nth是要删除的发生量。

Just in case somebody needs both "this" and "those.that" in a way as alex described in his comment , here is a modified code: 为了防止有人像他的评论中描述的那样需要“this”和“that.that”,这里有一个修改过的代码:

 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; 

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这样简单的代码。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM