Billy Edson
Billy Edson

Reputation: 33

split a string and append to it another without comma char jquery

I split a string and then I wanted to append another to it but it is automatically added a comma char between the split string and the appended one, below is an example:

var mystring="mypath/myfile0.png"
var myotherstring=mystring.split(".png")+"1.png"

The result is: myotherstring=mypath/myfile0,1.png please pay attention to the comma character. How can I append a string after splitting another without this comma char? why is it being added that comma char?

Edson

Upvotes: 0

Views: 467

Answers (2)

Jai
Jai

Reputation: 74738

.join() it:

var myotherstring=mystring.split(".png").join("1.png");

This outputs mypath/myfile01.png

Demo for above one

And if you want get rid of 0 then use this:

var myotherstring=mystring.split("0.png").join("1.png");

This outputs mypath/myfile1.png

Demo for above one

Upvotes: 1

Replace your following line:

var myotherstring=mystring.split(".png")+"1.png"

for this one:

var myotherstring=mystring.split(".png")[0]+"1.png"

As you were concatenating to the whole array returned by .split() instead of just appending to the first element in the arrray which is what you wanted.

See demo

Upvotes: 1

Related Questions