Remove leading comma from a string

JavascriptArraysString

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

One-liner

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

I'll be back.

Solution 4 - Javascript

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.

Solution 5 - Javascript

Assuming the string is called 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.

Solution 6 - Javascript

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.

Solution 7 - Javascript

To turn a string into an array I usually use 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.

Solution 8 - Javascript

s=s.substring(1);

I like to keep stuff simple.

Solution 9 - Javascript

You can use directly replace function on javascript with regex or define a help function as in php ltrim(left) and rtrim(right):

  1. With replace:

    var myArray = ",'first string','more','even more'".replace(/^\s+/, '').split(/'?,?'/);

  2. Help functions:

    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.

Solution 10 - Javascript

this will remove the trailing commas and spaces

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

Solution 11 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMirceaView Question on Stackoverflow
Solution 1 - JavascriptJoel EthertonView Answer on Stackoverflow
Solution 2 - JavascriptthomasrutterView Answer on Stackoverflow
Solution 3 - JavascriptPawelView Answer on Stackoverflow
Solution 4 - JavascriptEMMERICHView Answer on Stackoverflow
Solution 5 - JavascriptjjrvView Answer on Stackoverflow
Solution 6 - JavascriptpeterhilView Answer on Stackoverflow
Solution 7 - JavascriptFabian JakobsView Answer on Stackoverflow
Solution 8 - JavascriptCrisView Answer on Stackoverflow
Solution 9 - JavascriptonalbiView Answer on Stackoverflow
Solution 10 - JavascriptAvinashView Answer on Stackoverflow
Solution 11 - JavascriptJustin KruseView Answer on Stackoverflow