Frequently Used String Extension Methods in C#

Introduction

You may have learned about extension methods in my previous article Extension Method In C# that explained their step-by-step implementation. Therefore, we will now go straight to code. 

Let us see each one by one.

SEO Friendly URL

Here, SEO friendly URL means an URL that has a dash (-) rather than a space between two words and special strings are converted to an appropriate string like C# converts in C#.
  1. Create a static class StringExtension that contains an extension method. This extension method converts a title in a SEO friendly URL.
    1. using System.Collections.Generic;  
    2. using System.Text;  
    3. using System.Text.RegularExpressions;  
    4.   
    5. namespace ExtensionMethod  
    6. {  
    7.     public static class StringExtension  
    8.     {  
    9.         public static string SeoFriendlyURL(this string title, int maxLength)  
    10.         {  
    11.             Dictionary<stringstring> dictWords = new Dictionary<stringstring>{  
    12.                 {"C#","c-sharp"},  
    13.                 {"F#","f-sharp"}  
    14.             };  
    15.   
    16.             foreach (KeyValuePair<stringstring> word in dictWords)  
    17.             {  
    18.                 title = title.Replace(word.Key, word.Value);  
    19.             }  
    20.   
    21.             var match = Regex.Match(title.ToLower(), "[\\w]+");  
    22.             StringBuilder seoUrl = new StringBuilder("");  
    23.             bool maxLengthHit = false;  
    24.             while (match.Success && !maxLengthHit)  
    25.             {  
    26.                 if (seoUrl.Length + match.Value.Length <= maxLength)  
    27.                 {  
    28.                     seoUrl.Append(match.Value + "-");  
    29.                 }  
    30.                 else  
    31.                 {  
    32.                     maxLengthHit = true;  
    33.                     if (seoUrl.Length == 0)  
    34.                     {  
    35.                         seoUrl.Append(match.Value.Substring(0, maxLength));  
    36.                     }  
    37.                 }  
    38.                 match = match.NextMatch();  
    39.             }  
    40.             if (seoUrl[seoUrl.Length - 1] == '-')  
    41.             {  
    42.                 seoUrl.Remove(seoUrl.Length - 1, 1);  
    43.             }  
    44.             return seoUrl.ToString();  
    45.         }  
    46.     }  
    47. }  
  2. The following code snippet shows how to call it.
    1. using System;  
    2.   
    3. namespace ExtensionMethod  
    4. {  
    5.     class Program  
    6.     {  
    7.         static void Main(string[] args)  
    8.         {  
    9.             Console.WriteLine("Enter the Title");  
    10.             string title = Console.ReadLine();  
    11.             string seoUrl = title.SeoFriendlyURL(100);  
    12.             Console.WriteLine("Seo url is: {0}", seoUrl);  
    13.             Console.ReadKey();  
    14.         }  
    15.     }  
    16. }  
    Output for SEO friendly URL
    Figure 1.1 : Output for SEO friendly URL

String Exists in String Array

Here we check a string that either exists or not in a defined array.

  1. Create a static class StringExtension that contains an extension method. This extension method returns a true value if the string exists in a defined array else returns false.
    1. using System;  
    2. using System.Linq;  
    3.   
    4. namespace ExtensionMethod  
    5. {  
    6.     public static class StringExtension  
    7.     {  
    8.         public static bool IsExist(this string input, params string[] values)  
    9.         {  
    10.             return String.IsNullOrEmpty(input) ? false : values.Any(S => input.Contains(S));  
    11.         }  
    12.     }  
    13. }  
  2. The following code snippet shows how to call it.
    1. using System;  
    2.   
    3. namespace ExtensionMethod  
    4. {  
    5.     class Program  
    6.     {  
    7.         static void Main(string[] args)  
    8.         {  
    9.             string[] language = new string[] { "C#""F#""Java" };  
    10.             Console.WriteLine("Enter a lagunage name");  
    11.             string name = Console.ReadLine();  
    12.             bool isExist = name.IsExist(language);  
    13.             Console.WriteLine("{0} is exist: {1}", name, isExist);  
    14.             Console.WriteLine("Enter another lagunage name");  
    15.              name = Console.ReadLine();  
    16.              isExist = name.IsExist(language);  
    17.             Console.WriteLine("{0} is exist: {1}", name, isExist);  
    18.             Console.ReadKey();  
    19.         }  
    20.     }  
    21. }  
    Output Extension method
    Figure 1.2: Output Extension method
Create Dotted String

Sometimes we have a situation where we don't want to show an entire string, instead we show a portion of a string followed by dots (…).
  1. Create a static class StringExtension that contains an extension method. This extension method returns either a dotted string or the full string depending on the conditions.
    1. using System;  
    2. using System.Linq;  
    3.   
    4. namespace ExtensionMethod  
    5. {  
    6.     public static class StringExtension  
    7.     {  
    8.         public static string DottedString(this string input, int length, bool Incomplete = true)  
    9.         {  
    10.             if (String.IsNullOrEmpty(input))  
    11.             {  
    12.                 return String.Empty;  
    13.             }  
    14.             return input.Length > length ? String.Concat(input.Substring(0, length), Incomplete ? "..." : "") : input;  
    15.         }  
    16.     }  
    17. }  
  2. The following code snippet shows how to call it.
    1. using System;  
    2.   
    3. namespace ExtensionMethod  
    4. {  
    5.     class Program  
    6.     {  
    7.         static void Main(string[] args)  
    8.         {  
    9.             Console.WriteLine("Enter a string value");  
    10.             string value = Console.ReadLine();  
    11.             string dottedString = value.DottedString(5);  
    12.             Console.WriteLine("{0} is converted: {1}", value, dottedString);  
    13.             Console.WriteLine("Enter another string value");  
    14.             value = Console.ReadLine();  
    15.             dottedString = value.DottedString(5, false);//trim without dot  
    16.             Console.WriteLine("{0} is converted: {1}", value, dottedString);  
    17.             Console.ReadKey();  
    18.         }  
    19.     }  
    20. }  
    Output for dotted string
    Figure 1.3: Output for dotted string

Up Next
    Ebook Download
    View all
    Learn
    View all