DEV Community

Supraja Tangella
Supraja Tangella

Posted on

๐—ฆ๐˜๐—ฟ๐—ถ๐—ป๐—ด ๐—˜๐˜…๐˜๐—ฒ๐—ป๐˜€๐—ถ๐—ผ๐—ป ๐— ๐—ฒ๐˜๐—ต๐—ผ๐—ฑ๐˜€ ๐—ง๐—ต๐—ฎ๐˜ ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ถ๐—ณ๐—ถ๐—ฒ๐—ฑ ๐— ๐˜† ๐—–๐—ผ๐—ฑ๐—ฒ ๐—ถ๐—ป ๐—”๐—ฆ๐—ฃ.๐—ก๐—˜๐—ง ๐—–๐—ผ๐—ฟ๐—ฒ

Have you ever repeated the same string manipulation logic across multiple files? I didโ€”until I learned the power of string extensions.

While building the Employee Performance Review System, I created some helpful string extension methods to make my code cleaner and more reusable.

๐Ÿ’ก 1. ๐—–๐—ผ๐—ป๐˜ƒ๐—ฒ๐—ฟ๐˜ ๐˜๐—ผ ๐—ง๐—ถ๐˜๐—น๐—ฒ ๐—–๐—ฎ๐˜€๐—ฒ

๐Ÿง  ๐—™๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐—ถ๐˜๐˜†: Capitalizes the first letter of each word in a string (e.g., full names).

var name = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input.ToLower());
// "john doe" โ†’ "John Doe"

๐Ÿ’ก 2. ๐—ฆ๐˜๐—ฟ๐—ถ๐—ฝ ๐—›๐—ง๐— ๐—Ÿ ๐—ง๐—ฎ๐—ด๐˜€

๐Ÿง  ๐—™๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐—ถ๐˜๐˜†: Removes all HTML tags from a string to display plain text.

var plainText = Regex.Replace(input, "<.*?>", string.Empty);
// "

Hello

" โ†’ "Hello"

๐Ÿ’ก ๐Ÿฏ. ๐—–๐—ต๐—ฒ๐—ฐ๐—ธ ๐—ถ๐—ณ ๐—ฆ๐˜๐—ฟ๐—ถ๐—ป๐—ด ๐—ถ๐˜€ ๐—ก๐˜‚๐—บ๐—ฒ๐—ฟ๐—ถ๐—ฐ

๐Ÿง  ๐—™๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น๐—ถ๐˜๐˜†: Validates whether a string contains only digits.

bool isNumeric = input.All(char.IsDigit);
// "12345" โ†’ true, "12a45" โ†’ false

๐—ช๐—ต๐˜† ๐—œ ๐—จ๐˜€๐—ฒ ๐—ฆ๐˜๐—ฟ๐—ถ๐—ป๐—ด ๐—˜๐˜…๐˜๐—ฒ๐—ป๐˜€๐—ถ๐—ผ๐—ป๐˜€:

ย ย โ€ข Improves ๐—ฐ๐—ผ๐—ฑ๐—ฒ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ฎ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜†

ย ย โ€ข Promotes ๐——๐—ฅ๐—ฌ (๐——๐—ผ๐—ป'๐˜ ๐—ฅ๐—ฒ๐—ฝ๐—ฒ๐—ฎ๐˜ ๐—ฌ๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐—น๐—ณ) principles

ย ย โ€ข Makes unit testing easier

These methods are now part of my helper library and reused across multiple layers โ€” from view models to services.

๐Ÿ’ฌ ๐—ช๐—ต๐—ฎ๐˜ ๐˜€๐˜๐—ฟ๐—ถ๐—ป๐—ด ๐—ต๐—ฒ๐—น๐—ฝ๐—ฒ๐—ฟ๐˜€ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐˜‚๐˜€๐—ฒ ๐—ผ๐—ณ๐˜๐—ฒ๐—ป ๐—ถ๐—ป ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ฝ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜๐˜€?

Top comments (0)