Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 39 / 68

Normalizing strings (trimming whitespace, unifying case)

This lesson covers normalizing strings in Objective-C, with the aim of comparing text that is written inconsistently.

Text typed by a user tends to carry stray whitespace at the ends and inconsistent capitalisation. Stripping it with stringByTrimmingCharactersInSet: and unifying the case with lowercaseString is called normalization.

The sample code strips the surrounding whitespace with [NSCharacterSet whitespaceCharacterSet] inside a normalize() function and then lower-cases with lowercaseString. Two strings that look different — " Hello World " and "hello world" — compare as the same once normalized.

A common early stumble is comparing without this step. Two strings that look identical to a person can come back false, which makes for a bug that is hard to track down.

In professional work, normalization is a basic technique that turns up constantly — validating input, handling logins.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)