javascript 从Javascript中的逗号分隔值中获取字符串的第一个和其余部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13341016/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:29:21  来源:igfitidea点击:

Get first and rest of string from comma separated values in Javascript

javascriptstring

提问by Hommer Smith

Given a string like this:

给定一个这样的字符串:

"boy, girl, dog, cat"

What would be a good way to get the first word and the rest of them, so I could have this:

获得第一个单词和其余单词的好方法是什么,所以我可以这样:

var first_word = "boy";
var rest = "girl, dog, cat";

Right now I have this:

现在我有这个:

my_string.split(",");But that gives me all the words that are between the commas.

my_string.split(",");但这给了我逗号之间的所有单词。

采纳答案by Zeta

You can use both splitand splice:

您可以同时使用splitsplice

var str = "boy, girl, dog, cat";
var arr = str.split(",");
var fst = arr.splice(0,1).join("");
var rest = arr.join(",");

Or similar

或类似的

// returns an array with the extracted str as first value
// and the rest as second value
function cutFirst(str,token){
   var arr = str.split(token);
   var fst = arr.splice(0,1);
   return [fst.join(""),arr.join(token)];
}

回答by FixMaker

You can use a regex to match the two strings before and after the first comma:

您可以使用正则表达式来匹配第一个逗号前后的两个字符串:

var v = "boy, girl, dog, cat";
var strings = v.match(/([^,]*),(.*)/);

Now stringswill be a three element array where

现在strings将是一个三元素数组,其中

  • strings[0]contains the full string
  • strings[1]contains the string before the first comma
  • strings[2]contains everything after the first comma
  • strings[0]包含完整的字符串
  • strings[1]包含第一个逗号前的字符串
  • strings[2]包含第一个逗号之后的所有内容

回答by Sirko

Use a combination of substring()(returning the substring between two indexes or the end of the string) and indexOf()(returning the first position of a substring within another string):

使用substring()(返回两个索引之间或字符串末尾的子字符串)和indexOf()(返回另一个字符串中子字符串的第一个位置)的组合:

var input = "boy, girl, dog, cat",
    pos = input.indexOf( ',' );

console.log( input.substring( 0, pos ) );
console.log( input.substring( pos + 1 ) );

Maybe you want to add an trim()call to the results to remove whitespaces.

也许您想添加trim()对结果的调用以删除空格。

回答by 1.44mb

Yet another option is to split the array, get first and join the rest:

另一种选择是拆分数组,先获取并加入其余部分:

var my_string = "boy, girl, dog, cat";
var splitted = my_string.split(',');
var first_item = splitted.shift();
var rest_of_the_items = splitted.join(',');

Here is the JSFiddle.

这是JSFiddle

回答by 1.44mb

You can create an array in a lazy way and then retrieve just the first element.

您可以以懒惰的方式创建一个数组,然后只检索第一个元素。

var ArrayFromString = "boy, girl, dog, cat".split(",");
firstElement = ArrayFromString.pop();
alert(firstElement); //boy
alert(ArrayFromString); //girl, dog, cat?????????????????