The snippet library.
Small, practical examples. Search by task or narrow things down below.
Check whether text ends with a suffix
TypeScript: Use endsWith for a literal case-sensitive ending.
TypeScript✓ Tested
Check whether text starts with a prefix
TypeScript: Use startsWith for a literal case-sensitive prefix comparison.
TypeScript✓ Tested
Compare canonically equivalent Unicode text
TypeScript: Normalize to NFC before comparing equivalent composed and decomposed text.
TypeScript✓ Tested
Count Unicode code points in a string
TypeScript: String iteration combines surrogate pairs, unlike the UTF-16 length property.
TypeScript✓ Tested
Extract text by start and end positions
TypeScript: String slice uses an inclusive start and exclusive end measured in UTF-16 code units.
TypeScript✓ Tested
Extract the text after the last delimiter
TypeScript: Use lastIndexOf when earlier occurrences should remain part of the prefix.
TypeScript✓ Tested
Normalize identifier case consistently
TypeScript: Use toLowerCase for a simple case-insensitive ASCII identifier lookup.
TypeScript✓ Tested
Repeat a string a fixed number of times
TypeScript: String.repeat requires a nonnegative finite repetition count.
TypeScript✓ Tested
Transform each regex match with a callback
TypeScript: A replacement callback computes the replacement separately for each match.
TypeScript✓ Tested
Trim and discard blank lines
TypeScript: Split a text block, trim each line, and keep nonempty values.
TypeScript✓ Tested