Update and rename changeStringToArrayOfChars.md to toCharArray.md
This commit is contained in:
@ -1,20 +0,0 @@
|
|||||||
---
|
|
||||||
title: changeStringToArrayOfChars
|
|
||||||
tags: array,intermediate
|
|
||||||
---
|
|
||||||
|
|
||||||
Function takes string as a parameter and returns an array of characters.
|
|
||||||
|
|
||||||
- Array.prototype.split() divides a given string to separate characters
|
|
||||||
- In this case `split()` takes in an empty string as an argument `str.split('')`
|
|
||||||
- Separator can be a string or a regural expression
|
|
||||||
|
|
||||||
```js
|
|
||||||
const changeStringToArrayOfChars = text => {
|
|
||||||
return text.split('');
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```js
|
|
||||||
changeStringToArrayOfChars('jsisawesome'); // ["j", "s", "i", "s", "a", "w", "e", "s", "o", "m", "e"]
|
|
||||||
```
|
|
||||||
16
snippets/toCharArray.md
Normal file
16
snippets/toCharArray.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
---
|
||||||
|
title: toCharArray
|
||||||
|
tags: string,beginner
|
||||||
|
---
|
||||||
|
|
||||||
|
Converts a string to an array of characters.
|
||||||
|
|
||||||
|
- Use the spread operator (`...`) to convert the string into an array of characters.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const toCharArray = s => [...s];
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
toCharArray('hello'); // ['h', 'e', 'l', 'l', 'o']
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user