How to Convert Comma Separated String to Array in Angular?

By Hardik Savani May 2, 2024 Category : Angular

Here, i will show you how to works how to convert array into comma separated string in angular. you will learn convert string into array angular. if you want to see example of convert comma separated string to array angular then you are a right place. In this article, we will implement a convert comma separated string to json array angular.

I will give you very simple example of convert comma separated string to array angular application. you can easily convert string to array in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 project.

we will use split() function to converting string to array in angular app. split() provide to covert comma separated string to array. so let's see bellow example that will help you easily understand.

Syntax:

split(separator, limit)

you can see example with output:

src/app/app.component.ts

import { Component } from '@angular/core';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular';

myString = 'Hardik,Paresh,Vimal,Harshad,Kiran';

myArray = this.myString.split(',');

constructor() {

console.log(this.myArray);

}

}

Output:

["Hardik", "Paresh", "Vimal", "Harshad", "Kiran"]

0: "Hardik"

1: "Paresh"

2: "Vimal"

3: "Harshad"

4: "Kiran"

I hope it can help you...

Shares