简体   繁体   English

无论字符串中出现的吐出字符数是多少,Javascript都会拆分为2个部分

[英]Javascript split to split string in 2 parts irrespective of number of spit characters present in string

I want to split a string in Javascript using split function into 2 parts. 我想使用split函数将Javascript中的字符串拆分为2部分。

For Example i have string: 例如我有字符串:

str='123&345&678&910'

If i use the javascripts split, it split it into 4 parts. 如果我使用javascripts split,它将它分成4个部分。 But i need it to be in 2 parts only considering the first '&' which it encounters. 但是我只需考虑它遇到的第一个'&'而需要它分为两个部分。

As we have in Perl split, if i use like: 正如我们在Perl拆分中所做的那样,如果我使用:

($fir, $sec) = split(/&/,str,2)

it split's str into 2 parts, but javascript only gives me: 它将str分为两部分,但是javascript只给了我:

str.split(/&/, 2);
fir=123
sec=345

i want sec to be: 我希望秒为:

sec=345&678&910

How can i do it in Javascript. 我怎么能在Javascript中做到这一点。

var subStr = string.substring(string.indexOf('&') + 1);

View this similar question for other answers: 查看此类似问题的其他答案:

split string only on first instance of specified character 仅在指定字符的第一个实例上拆分字符串

You can use match instead of split : 您可以使用match而不是split

str='123&345&678&910';
splited = str.match(/^([^&]*?)&(.*)$/);
splited.shift();
console.log(splited);

output: 输出:

["123", "345&678&910"]

You can remain on the split part by using the following trick: 您可以使用以下技巧保留在split部分:

var str='123&345&678&910',
    splitted = str.split( '&' ),
    // shift() removes the first item and returns it
    first = splitted.shift();

console.log( first ); // "123"
console.log( splitted.join( '&' ) ); // "345&678&910"

I wrote this function: 我写了这个函数:

function splitter(mystring, mysplitter) {
    var myreturn = [],
        myindexplusone = mystring.indexOf(mysplitter) + 1;

    if (myindexplusone) {
        myreturn[0] = mystring.split(mysplitter, 1)[0];
        myreturn[1] = mystring.substring(myindexplusone);
    }

    return myreturn;
}

var str = splitter("hello-world-this-is-a-test", "-");

console.log(str.join("<br>"));
//hello
//world-this-is-a-test​​​

The output will be either an empty array (not match) or an array with 2 elements (before the split and everything after) 输出将是一个空数组(不匹配)或一个包含2个元素的数组(在拆分之前和之后的所有内容)

Demo 演示

I have that: 我有这个:

var str='123&345&678&910';
str.split('&',1).concat( str.split('&').slice(1).join('&') );
//["123", "345&678&910"]



str.split('&',2).concat( str.split('&').slice(2).join('&') );
//["123", "345", "678&910"];

for convenience: 为了方便:

String.prototype.mySplit = function( sep, chunks) {
   chunks = chunks|=0 &&chunks>0?chunks-1:0;
   return this.split( sep, chunks )
              .concat( 
                  chunks?this.split( sep ).slice( chunks ).join( sep ):[]
               );
}

What about the use of split() and replace() ?: 如何使用split()replace() ?:

Given we have that string str='123&345&678&910' We can do 鉴于我们有字符串str='123&345&678&910'我们可以做到

    var first = str.split("&",1); //gets the first word
    var second = str.replace(first[0]+"&", ""); //removes the first word and the ampersand

Please note that split() returns an array that is why getting the index with first[0] is recommended , however, without getting the index, it still worked as needed ie first+"&" . 请注意, split()返回一个数组,这就是为什么建议使用first[0]获取索引,但是,如果没有得到索引,它仍然按需要工作,即first+"&"

Feel free to replace the "&" with the string you need to split with. 随意用你需要拆分的字符串替换“&”。

Hope this helps :) 希望这可以帮助 :)

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

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