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)