Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 39 / 68

Normalizing Strings (Trim, Unifying Case)

In this lesson you will learn how to normalize strings in C#, so inconsistent formatting can still be compared. This is for anyone searching for "C# string normalization".

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

The sample code runs s.Trim().ToLower() inside a Normalize() function, removing surrounding whitespace and then folding to lower case. You can see two strings that look different, " Hello World " and "hello world", compare as equal once normalized.

A common stumbling block for beginners is comparing strings without doing this first. Two values that look identical to a human can still come back as False.

In real-world development, there is also a more explicit approach: passing StringComparison.OrdinalIgnoreCase to a string comparison.

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

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