Javascript 从字符串中删除前导逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3923015/
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
Remove leading comma from a string
提问by Mircea
I have the following string:
我有以下字符串:
",'first string','more','even more'"
I want to transform this into an Array but obviously this is not valid due to the first comma. How can I remove the first comma from my string and make it a valid Array?
我想将其转换为数组,但显然由于第一个逗号,这无效。如何从字符串中删除第一个逗号并使其成为有效数组?
I'd like to end up with something like this:
我想以这样的方式结束:
myArray = ['first string','more','even more']
回答by Joel Etherton
To remove the first character you would use:
要删除您将使用的第一个字符:
var myOriginalString = ",'first string','more','even more'";
var myString = myOriginalString.substring(1);
I'm not sure this will be the result you're looking for though because you will still need to split it to create an array with it. Maybe something like:
我不确定这将是您正在寻找的结果,因为您仍然需要拆分它以使用它创建一个数组。也许是这样的:
var myString = myOriginalString.substring(1);
var myArray = myString.split(',');
Keep in mind, the ' character will be a part of each string in the split here.
请记住,' 字符将成为此处拆分中每个字符串的一部分。
回答by thomasrutter
In this specific case (there is always a single character at the start you want to remove) you'll want:
在这种特定情况下(您要删除的开头总是有一个字符),您需要:
str.substring(1)
However, if you want to be able to detect if the comma is there and remove it if it is, then something like:
但是,如果您希望能够检测逗号是否存在并将其删除(如果存在),则类似于:
if (str[0] == ',') {
str = str.substring(1);
}
回答by Pawel
One-liner
单线
str = str.replace(/^,/, '');
I'll be back.
我会回来的。
回答by EMMERICH
var s = ",'first string','more','even more'";
var array = s.split(',').slice(1);
That's assuming the string you begin with is in fact a String, like you said, and not an Array of strings.
那是假设你开始的字符串实际上是一个字符串,就像你说的,而不是一个字符串数组。
回答by jjrv
Assuming the string is called myStr:
假设字符串名为 myStr:
// Strip start and end quotation mark and possible initial comma
myStr=myStr.replace(/^,?'/,'').replace(/'$/,'');
// Split stripping quotations
myArray=myStr.split("','");
Note that if a string can be missing in the list without even having its quotation marks present and you want an empty spot in the corresponding location in the array, you'll need to write the splitting manually for a robust solution.
请注意,如果列表中可能缺少一个字符串,甚至连引号都不存在,并且您希望在数组中的相应位置有一个空位,则您需要手动编写拆分以获得可靠的解决方案。
回答by peterhil
var s = ",'first string','more','even more'";
s.split(/'?,'?/).filter(function(v) { return v; });
Results in:
结果是:
["first string", "more", "even more'"]
First split with commas possibly surrounded by single quotes,
then filter the non-truthy (empty) parts out.
首先用逗号分隔,可能用单引号括起来,
然后过滤掉非真实(空)部分。
回答by Fabian Jakobs
To turn a string into an array I usually use split()
要将字符串转换为数组,我通常使用 split()
> var s = ",'first string','more','even more'"
> s.split("','")
[",'first string", "more", "even more'"]
This is almost what you want. Now you just have to strip the first two and the last character:
这几乎就是你想要的。现在你只需要去掉前两个和最后一个字符:
> s.slice(2, s.length-1)
"first string','more','even more"
> s.slice(2, s.length-2).split("','");
["first string", "more", "even more"]
To extract a substring from a string I usually use slice()
but substr()
and substring()
also do the job.
为了从一个字符串,我通常使用一个子slice()
,但substr()
并substring()
还做的工作。
回答by onalbi
You can use directly replace function on javascript with regex or define a help function as in php ltrim(left) and rtrim(right):
您可以使用正则表达式直接在 javascript 上使用替换函数,或者像在 php ltrim(left) 和 rtrim(right) 中定义帮助函数:
1) With replace:
1) 替换:
var myArray = ",'first string','more','even more'".replace(/^\s+/, '').split(/'?,?'/);
2) Help functions:
2) 帮助功能:
if (!String.prototype.ltrim) String.prototype.ltrim = function() {
return this.replace(/^\s+/, '');
};
if (!String.prototype.rtrim) String.prototype.rtrim = function() {
return this.replace(/\s+$/, '');
};
var myArray = ",'first string','more','even more'".ltrim().split(/'?,?'/).filter(function(el) {return el.length != 0});;
You can do and other things to add parameter to the help function with what you want to replace the char, etc.
您可以做其他事情来将参数添加到帮助功能中,并使用您想要替换的字符等。
回答by Cris
s=s.substring(1);
I like to keep stuff simple.
我喜欢保持简单。
回答by Avinash
this will remove the trailing commas and spaces
这将删除尾随的逗号和空格
var str = ",'first string','more','even more'";
var trim = str.replace(/(^\s*,)|(,\s*$)/g, '');