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#.
- Create a static class StringExtension that contains an extension method. This extension method converts a title in a SEO friendly URL.
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
-
- namespace ExtensionMethod
- {
- public static class StringExtension
- {
- public static string SeoFriendlyURL(this string title, int maxLength)
- {
- Dictionary<string, string> dictWords = new Dictionary<string, string>{
- {"C#","c-sharp"},
- {"F#","f-sharp"}
- };
-
- foreach (KeyValuePair<string, string> word in dictWords)
- {
- title = title.Replace(word.Key, word.Value);
- }
-
- var match = Regex.Match(title.ToLower(), "[\\w]+");
- StringBuilder seoUrl = new StringBuilder("");
- bool maxLengthHit = false;
- while (match.Success && !maxLengthHit)
- {
- if (seoUrl.Length + match.Value.Length <= maxLength)
- {
- seoUrl.Append(match.Value + "-");
- }
- else
- {
- maxLengthHit = true;
- if (seoUrl.Length == 0)
- {
- seoUrl.Append(match.Value.Substring(0, maxLength));
- }
- }
- match = match.NextMatch();
- }
- if (seoUrl[seoUrl.Length - 1] == '-')
- {
- seoUrl.Remove(seoUrl.Length - 1, 1);
- }
- return seoUrl.ToString();
- }
- }
- }
- The following code snippet shows how to call it.
- using System;
-
- namespace ExtensionMethod
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter the Title");
- string title = Console.ReadLine();
- string seoUrl = title.SeoFriendlyURL(100);
- Console.WriteLine("Seo url is: {0}", seoUrl);
- Console.ReadKey();
- }
- }
- }
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.
- 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.
- using System;
- using System.Linq;
-
- namespace ExtensionMethod
- {
- public static class StringExtension
- {
- public static bool IsExist(this string input, params string[] values)
- {
- return String.IsNullOrEmpty(input) ? false : values.Any(S => input.Contains(S));
- }
- }
- }
- The following code snippet shows how to call it.
- using System;
-
- namespace ExtensionMethod
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] language = new string[] { "C#", "F#", "Java" };
- Console.WriteLine("Enter a lagunage name");
- string name = Console.ReadLine();
- bool isExist = name.IsExist(language);
- Console.WriteLine("{0} is exist: {1}", name, isExist);
- Console.WriteLine("Enter another lagunage name");
- name = Console.ReadLine();
- isExist = name.IsExist(language);
- Console.WriteLine("{0} is exist: {1}", name, isExist);
- Console.ReadKey();
- }
- }
- }
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 (…).
- 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.
- using System;
- using System.Linq;
-
- namespace ExtensionMethod
- {
- public static class StringExtension
- {
- public static string DottedString(this string input, int length, bool Incomplete = true)
- {
- if (String.IsNullOrEmpty(input))
- {
- return String.Empty;
- }
- return input.Length > length ? String.Concat(input.Substring(0, length), Incomplete ? "..." : "") : input;
- }
- }
- }
- The following code snippet shows how to call it.
- using System;
-
- namespace ExtensionMethod
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter a string value");
- string value = Console.ReadLine();
- string dottedString = value.DottedString(5);
- Console.WriteLine("{0} is converted: {1}", value, dottedString);
- Console.WriteLine("Enter another string value");
- value = Console.ReadLine();
- dottedString = value.DottedString(5, false);
- Console.WriteLine("{0} is converted: {1}", value, dottedString);
- Console.ReadKey();
- }
- }
- }
Figure 1.3: Output for dotted string