Menu

How To Convert Array To Comma Separated String In Angular

Hello,
Today I will show you how we can convert an array to a string. If we want to know how to convert an array to a string in angular then we are at the right place. I will give share an example to show how we can convert an array to a comma separate string.

This example will work for Angular 7, Angular 8, Angular 9, Angular 10, Angular 11, Angular 12, Angular 13, Angular 14, Angular 15, and Angular 16.

we will use the toString() array function to convert the array to a string in angular app. toString() provide to covert array into comma separated string. so let's see the below example that will help you easily understand.

Let's have an 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';
 
  fruits = ['Apple','Mango','Banana'];
  myString = this.fruits.toString();
  
}

src/app/app.component.html

<h1>{{ myString }}</h1>
<h2>{{ fruits.toString() }}</h2>

Output:

Apple,Mango,Banana
Apple,Mango,Banana

I hope this example we are helpful to you.
Please share this post with your friends.
 

1124
Search

Ads