Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Created June 17, 2015 09:46
Show Gist options
  • Save afonsomatos/b6841476f0ecf12a9135 to your computer and use it in GitHub Desktop.
Save afonsomatos/b6841476f0ecf12a9135 to your computer and use it in GitHub Desktop.
Most important String features in ES6
// If strings starts with substr
'myString'.startsWith('my', 0 /* start searching */); // true
// If string ends with substr
'myString'.endsWith('ring', 8 /* end searching */); // true
// If strings has another substr
'myString'.includes('str', 0 /* start searching */); // true
// Multiply string
'woof '.repeat(5);
// -- Template strings
// String interpolation
`504 * 389127 is ${ 504 * 389127}`;
// Multiple lines
`this is a string
very cool`
// Raw string prefix
String.raw`\n\n`; // \\n\\n
// Strings are iterable
for (let ch of 'abc'); // a.. b.. c..
// Spread string
[...'abc']; // ['a', 'b', 'c']
// Reverse strings
[...'eval'].reverse().join(''); // 'lave'
// -- Unicode and code points
String.fromCodePoint(...codePoints : number[]) : string
String.prototype.codePointAt(pos) : number
String.prototype.normalize(form? : string) : string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment