Split string on the first white space occurrence

后端 未结 13 2003
孤街浪徒
孤街浪徒 2020-11-28 20:07

I didn\'t get an optimized regex that split me a String basing into the first white space occurrence:

var str=\"72 tocirah sneab\";

I need

相关标签:
13条回答
  • 2020-11-28 20:53

    I needed a slightly different result.

    I wanted the first word, and what ever came after it - even if it was blank.

    str.substr(0, text.indexOf(' ') == -1 ? text.length : text.indexOf(' '));
    str.substr(text.indexOf(' ') == -1 ? text.length : text.indexOf(' ') + 1);
    

    so if the input is oneword you get oneword and ''.

    If the input is one word and some more you get one and word and some more.

    0 讨论(0)
  • 2020-11-28 20:55

    Late to the game, I know but there seems to be a very simple way to do this:

    const str = "72 tocirah sneab";
    const arr = str.split(/ (.*)/);
    console.log(arr);

    This will leave arr[0] with "72" and arr[1] with "tocirah sneab". Note that arr[2] will be empty, but you can just ignore it.

    For reference:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#Capturing_parentheses

    0 讨论(0)
  • 2020-11-28 20:59

    If you only care about the space character (and not tabs or other whitespace characters) and only care about everything before the first space and everything after the first space, you can do it without a regular expression like this:

    str.substr(0,str.indexOf(' ')); // "72"
    str.substr(str.indexOf(' ')+1); // "tocirah sneab"
    

    Note that if there is no space at all, then the first line will return an empty string and the second line will return the entire string. Be sure that is the behavior that you want in that situation (or that that situation will not arise).

    0 讨论(0)
  • 2020-11-28 21:00

    georg's solution is nice, but breaks if the string doesn't contain any whitespace. If your strings have a chance of not containing whitespace, it's safer to use .split and capturing groups like so:

    str_1 = str.split(/\s(.+)/)[0];  //everything before the first space
    str_2 = str.split(/\s(.+)/)[1];  //everything after the first space
    
    0 讨论(0)
  • 2020-11-28 21:03

    In ES6 you can also

    let [first, ...second] = str.split(" ")
    second = second.join(" ")
    
    0 讨论(0)
  • 2020-11-28 21:06

    Just split the string into an array and glue the parts you need together. This approach is very flexible, it works in many situations and it is easy to reason about. Plus you only need one function call.

    arr = str.split(' ');             // ["72", "tocirah", "sneab"]
    strA = arr[0];                    // "72"
    strB = arr[1] + ' ' + arr[2];     // "tocirah sneab"
    

    Alternatively, if you want to cherry-pick what you need directly from the string you could do something like this:

    strA = str.split(' ')[0];                    // "72";
    strB = str.slice(strA.length + 1);           // "tocirah sneab"
    

    Or like this:

    strA = str.split(' ')[0];                    // "72";
    strB = str.split(' ').splice(1).join(' ');   // "tocirah sneab"
    

    However I suggest the first example.

    Working demo: jsbin

    0 讨论(0)
提交回复
热议问题