Skip to content

Instantly share code, notes, and snippets.

@LGitHub-sprout
Created November 11, 2022 01:08
Show Gist options
  • Save LGitHub-sprout/392e52039a3e1c26db79d175519d935a to your computer and use it in GitHub Desktop.
Save LGitHub-sprout/392e52039a3e1c26db79d175519d935a to your computer and use it in GitHub Desktop.
String to array;
/* Manipulating strings */
// Convert string to array + uncommon unicode char strings
// https://dev.to/sanchithasr/6-ways-to-convert-a-string-to-an-array-in-javascript-1cjg
const exampleString = 'A car, a man, a maraca.'.toLowerCase().split('');
const lastItem = exampleString.length - 1;
// if (exampleString[lastItem] === '.') delete exampleString[lastItem];
console.log(exampleString)
exampleString.forEach((el) => {
if (el.includes(',') || el.includes(' ') || el.includes('.')) return;
console.log(el)
});
// duplicate and then compare both sets?
const origString = 'A car, a man, a maraca.';
// const stringToArr = [...origString];
// const stringToArr = Array.from(origString);
// const stringToArr = Object.assign([], origString);
// loop
const arr = [];
// 'in' and 'of' produce VERY diff results
for (let oS of origString) arr.push(oS);
console.log('arr', arr)
// console.log('stringToArr', stringToArr)
const emojis = '😹🍷'; // beware special chars in some of above
const emojisArr = emojis.split(''); // unicode
console.log('emojisArr', emojisArr);
for (let e of emojis) console.log('emojis', e);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment