0

I have a string which contains values like

var string = A,B,C

Here I want to add single quote for each comma value, expected result should be as below

output = 'A','B','C'

My angular code is

var data = {
           output : this.string.split("\',"),
           }

which is giving result as

["A,B,C"]

Could anyone please help on this how can I get desired output.

3
  • which is giving result as ["A,B,C"] Commented Sep 21, 2017 at 14:45
  • Do you want the final answer to be a string? Commented Sep 21, 2017 at 14:50
  • Yes, final answer must be like this 'A','B','C' Commented Sep 21, 2017 at 14:59

1 Answer 1

3

I am understanding your code as

var string = "A,B,C";  // because string should be in this format.

and you just need to replace "\'," from your split function to "," which will give you an array like this

var out = string.split(",");
console.log(out);

[ 'A', 'B', 'C' ]  // this is the output.

as split function searches for the given expression and split the string into array.

but if you just want to modify the string without making it in array then you can use the below trick

var out = "'" + string.replace(/,/g, "','") + "'";
console.log(out);

'A','B','C'        // result as u mentioned and this is of string type.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.