Lessons · TypeScript · cleaning and reshaping text
Cleaning and reshaping text
trim, toLowerCase, split, replace, startsWith return new strings; the original never changes.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
User input is messy. Every search, validator and parser normalises first.
How to think about it
What should the text look like before I compare it? Decide what the text should look like before you compare it, then chain: s.trim().toLowerCase().
Worked example
const raw = " Hello, World ";Padding and case we do not want.
const clean = raw.trim().toLowerCase();"hello, world".
clean.replace(",", "").split(" ")["hello", "world"].Your turn
Normalise an email for comparison.
const key = email.().toLowerCase();
Solve one with the tests running
The trap
replace replaces only the first match unless you use a global regex or replaceAll.
Practise cleaning and reshaping text on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.