EN
TypeScript - convert char array to string
0 points
In this article, we would like to show you how to convert char array to string in TypeScript.
Quick solution:
xxxxxxxxxx
1
const letters: string[] = ['A', 'B', 'C'];
2
3
const result: string = letters.join('');
4
5
console.log(result); // 'ABC'
In this example, we use join()
method with an empty string (''
) as a separator to convert the letters
char array to the result
string.
xxxxxxxxxx
1
const letters: string[] = ['A', 'B', 'C'];
2
3
const result: string = letters.join('');
4
5
console.log(result); // 'ABC'
Output:
xxxxxxxxxx
1
ABC
In this example, we use join()
method with various separators.
xxxxxxxxxx
1
const letters: string[] = ['A', 'B', 'C'];
2
3
const result1: string = letters.join('');
4
const result2: string = letters.join(' ');
5
const result3: string = letters.join('-');
6
const result4: string = letters.join('_');
7
8
console.log(result1); // 'ABC'
9
console.log(result2); // 'A B C'
10
console.log(result3); // 'A-B-C'
11
console.log(result4); // 'A_B_C'
Output:
xxxxxxxxxx
1
ABC
2
A B C
3
A-B-C
4
A_B_C