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. namespace ExtensionMethod
    5. {
    6. public static class StringExtension
    7. {
    8. public static string SeoFriendlyURL(this string title, int maxLength)
    9. {
    10. Dictionary<string, string> dictWords = new Dictionary<string, string>{
    11. {"C#","c-sharp"},
    12. {"F#","f-sharp"}
    13. };
    14. foreach (KeyValuePair<string, string> word in dictWords)
    15. {
    16. title = title.Replace(word.Key, word.Value);
    17. }
    18. var match = Regex.Match(title.ToLower(), "[\\w]+");
    19. StringBuilder seoUrl = new StringBuilder("");
    20. bool maxLengthHit = false;
    21. while (match.Success && !maxLengthHit)
    22. {
    23. if (seoUrl.Length + match.Value.Length <= maxLength)
    24. {
    25. seoUrl.Append(match.Value + "-");
    26. }
    27. else
    28. {
    29. maxLengthHit = true;
    30. if (seoUrl.Length == 0)
    31. {
    32. seoUrl.Append(match.Value.Substring(0, maxLength));
    33. }
    34. }
    35. match = match.NextMatch();
    36. }
    37. if (seoUrl[seoUrl.Length - 1] == '-')
    38. {
    39. seoUrl.Remove(seoUrl.Length - 1, 1);
    40. }
    41. return seoUrl.ToString();
    42. }
    43. }
    44. }
  2. The following code snippet shows how to call it.
    1. using System;
    2. namespace ExtensionMethod
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. Console.WriteLine("Enter the Title");
    9. string title = Console.ReadLine();
    10. string seoUrl = title.SeoFriendlyURL(100);
    11. Console.WriteLine("Seo url is: {0}", seoUrl);
    12. Console.ReadKey();
    13. }
    14. }
    15. }
    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. namespace ExtensionMethod
    4. {
    5. public static class StringExtension
    6. {
    7. public static bool IsExist(this string input, params string[] values)
    8. {
    9. return String.IsNullOrEmpty(input) ? false : values.Any(S => input.Contains(S));
    10. }
    11. }
    12. }
  2. The following code snippet shows how to call it.
    1. using System;
    2. namespace ExtensionMethod
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. string[] language = new string[] { "C#", "F#", "Java" };
    9. Console.WriteLine("Enter a lagunage name");
    10. string name = Console.ReadLine();
    11. bool isExist = name.IsExist(language);
    12. Console.WriteLine("{0} is exist: {1}", name, isExist);
    13. Console.WriteLine("Enter another lagunage name");
    14. name = Console.ReadLine();
    15. isExist = name.IsExist(language);
    16. Console.WriteLine("{0} is exist: {1}", name, isExist);
    17. Console.ReadKey();
    18. }
    19. }
    20. }
    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. namespace ExtensionMethod
    4. {
    5. public static class StringExtension
    6. {
    7. public static string DottedString(this string input, int length, bool Incomplete = true)
    8. {
    9. if (String.IsNullOrEmpty(input))
    10. {
    11. return String.Empty;
    12. }
    13. return input.Length > length ? String.Concat(input.Substring(0, length), Incomplete ? "..." : "") : input;
    14. }
    15. }
    16. }
  2. The following code snippet shows how to call it.
    1. using System;
    2. namespace ExtensionMethod
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. Console.WriteLine("Enter a string value");
    9. string value = Console.ReadLine();
    10. string dottedString = value.DottedString(5);
    11. Console.WriteLine("{0} is converted: {1}", value, dottedString);
    12. Console.WriteLine("Enter another string value");
    13. value = Console.ReadLine();
    14. dottedString = value.DottedString(5, false);//trim without dot
    15. Console.WriteLine("{0} is converted: {1}", value, dottedString);
    16. Console.ReadKey();
    17. }
    18. }
    19. }
    Output for dotted string
    Figure 1.3: Output for dotted string