简体   繁体   English

从字符串中删除前导逗号

[英]Remove leading comma from a string

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']

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.请记住,' 字符将成为此处拆分中每个字符串的一部分。

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);
}

One-liner单线

str = str.replace(/^,/, '');

I'll be back.我会回来的。

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.那是假设你开始的字符串实际上是一个字符串,就像你说的,而不是一个字符串数组。

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.然后过滤掉不真实(空)的部分。

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.请注意,如果列表中可能缺少一个字符串,甚至连引号都不存在,并且您希望在数组中的相应位置有一个空位,则您需要手动编写拆分以获得可靠的解决方案。

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()也可以完成这项工作。

s=s.substring(1);

我喜欢保持简单。

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.您可以做其他事情来将参数添加到帮助功能中,并使用您想要替换的字符等。

this will remove the trailing commas and spaces这将删除尾随的逗号和空格

var str = ",'first string','more','even more'";
var trim = str.replace(/(^\s*,)|(,\s*$)/g, '');

remove leading or trailing characters:删除前导或尾随字符:

function trimLeadingTrailing(inputStr, toRemove) {
    // use a regex to match toRemove at the start (^)
    // and at the end ($) of inputStr
    const re = new Regex(`/^${toRemove}|{toRemove}$/`);
    return inputStr.replace(re, '');
}

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

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