Ad space (banner)
๐Ÿ’ŽRuby Lessons
Lesson 39 / 68

Normalizing Strings (strip, Unifying Case)

In this lesson you will learn to normalize strings with strip and case folding so you can compare user input safely. This is for anyone searching for "Ruby strip how to use".

Text entered by a user tends to carry stray leading or trailing spaces and inconsistent capitalisation. Stripping the spaces with .strip and unifying the case with .downcase is called normalization.

The sample code combines both steps into one operation with str.strip.downcase inside a normalize method. You can see that " Hello World " and "hello world", which look quite different, compare as the same string once normalized.

A common stumbling block for beginners is applying the normalization to only one side of the comparison. Whenever you write string-comparison code, make a point of applying the same normalization to both strings.

There is also casecmp?, a method that ignores case more explicitly. Normalization is a basic technique you cannot do without in input validation and login handling.

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

๐Ÿงช This site can't compile or run Ruby 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)