Split string once in javascript?

前端 未结 13 914
南笙
南笙 2020-11-29 03:23

How can I split a string only once, i.e. make 1|Ceci n\'est pas une pipe: | Oui parse to: [\"1\", \"Ceci n\'est pas une pipe: | Oui\"]?

The

相关标签:
13条回答
  • 2020-11-29 03:47

    one liner and imo, simpler:

    var str = 'I | am super | cool | yea!';
    str.split('|').slice(1).join('|');
    

    This returns " am super | cool | yea!"

    0 讨论(0)
  • 2020-11-29 03:48

    You'd want to use String.indexOf('|') to get the index of the first occurrence of '|'.

    var i = s.indexOf('|');
    var splits = [s.slice(0,i), s.slice(i+1)];
    
    0 讨论(0)
  • 2020-11-29 03:48

    This one's a little longer, but it works like I believe limit should:

    function split_limit(inString, separator, limit){
        var ary = inString.split(separator);
        var aryOut = ary.slice(0, limit - 1);
        if(ary[limit - 1]){
            aryOut.push(ary.slice(limit - 1).join(separator));
        }
        return aryOut;
    }
    console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 1));
    console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 2));
    console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 3));
    console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 7));
    

    https://jsfiddle.net/2gyxuo2j/

    limit of Zero returns funny results, but in the name of efficiency, I left out the check for it. You can add this as the first line of the function if you need it:

    if(limit < 1) return [];
    
    0 讨论(0)
  • 2020-11-29 03:51

    Just as evil as most of the answers so far:

    var splits = str.split('|');
    splits.splice(1, splits.length - 1, splits.slice(1).join('|'));
    
    0 讨论(0)
  • 2020-11-29 03:52

    ES6 syntax allows a different approach:

    function splitOnce(s, on) {
       [first, ...rest] = s.split(on)
       return [first, rest.length > 0? rest.join(on) : null]
    }
    

    Which also handles the eventuality of the string not having a | by returning null rather than an empty string, which is more explicit.

    splitOnce("1|Ceci n'est pas une pipe: | Oui", "|")
    >>> ["1", "Ceci n'est pas une pipe: | Oui"]
    
    splitOnce("Celui-ci n'a pas de pipe symbol!", "|")
    >>> ["Celui-ci n'a pas de pipe symbol!", null]
    

    Pas de pipe? C'est null!

    I added this reply primarily so I could make a pun on the pipe symbol, but also to show off es6 syntax - its amazing how many people still don't use it...

    0 讨论(0)
  • 2020-11-29 03:56

    If the string doesn't contain the delimiter @NickCraver's solution will still return an array of two elements, the second being an empty string. I prefer the behavior to match that of split. That is, if the input string does not contain the delimiter return just an array with a single element.

    var splitOnce = function(str, delim) {
        var components = str.split(delim);
        var result = [components.shift()];
        if(components.length) {
            result.push(components.join(delim));
        }
        return result;
    };
    
    splitOnce("a b c d", " "); // ["a", "b c d"]
    splitOnce("a", " "); // ["a"]
    
    0 讨论(0)
提交回复
热议问题