Thursday, May 9, 2019

C-Sharp : A better Substring extension method to get a string within a string

Here's a Substring extension when you want to get a string within a string.

The aim here is get rid of nasty length calculation, which to me in the original method (see below) is counter intuitive. IMHO the 2nd argument should have been endIndex

public string Substring (int startIndex, int length);

Therefore, my Substring extension method gets rid of length calculations altogether.

public string Substring (string findString, enum CommonStringAction);
Instead you just supply the string you want to find (needle) in the search string (haystack) and supply it with a common action. 


Source Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;

public static class Program
{
  /// <summary>
        /// Define common patterns for substing actions
        /// </summary>
        public enum StrCut
        {
            BeforeLessStr = 0,
            BeforeInclStr = 1,
            AfterLessStr = 2,
            AfterInclStr = 4
        }
        /// <summary>
        /// Extend Susbstring with ability to pass a string and choose common actions surrounding that string.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="arg">string argument</param>
        /// <param name="e">enum</param>
        /// <returns></returns>
        /// <author>Wed 01-May-19 - metadataconsult@gmail.com</author>
        public static string Substring(this string s, string arg, StrCut e)
        {
            //Range checking, why do you neet a unit test
            if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(arg)) return string.Empty;
            if (arg.Length > s.Length) return string.Empty;

            int idxarg = s.IndexOf(arg);

            if (idxarg == -1) return string.Empty; //is arg found in string

            bool argfits = (idxarg + arg.Length) <= s.Length; //is arg within limit of end of string
            
            if (e.Equals(StrCut.BeforeLessStr))
            {
                s = s.Substring(0, idxarg);
            }
            else if (argfits && e.Equals(StrCut.BeforeInclStr))
            {
                s = s.Substring(0, (idxarg + arg.Length));
            }
            else if (argfits && e.Equals(StrCut.AfterLessStr))
            {
                s = s.Substring(idxarg + arg.Length);
            }
            else if (e.Equals(StrCut.AfterInclStr))
            {
                s = s.Substring(idxarg);
            }
            return s;
        }
 
 public static void Main()
 {
  string SourceURL = "SourceURL:https://metadataconsulting.blogspot.com/2019/04/Using-HTACG-HTML-Tidy-official-release-binaries-for-TidyHTML5Mananged.html"; 
  //string SURL = "SourceURL:";
  //string SURL = "SourceURL:https://metadataconsulting.blogspot.com/2019/04/Using-HTACG-HTML-Tidy-official-release-binaries-for-TidyHTML5Mananged.html"; 
  string SURL = ".html";
  Console.WriteLine("Let's get rid of length param, like this Substring(0) = " + SourceURL.Substring(0)); 
    
  Console.WriteLine("BEGIN - Most of time, I just want to get a supply a string a work around it"); 
  Console.WriteLine("org = "+ SourceURL); 
  Console.WriteLine("AfterLessStr = " + SourceURL.Substring(SURL,StrCut.AfterLessStr));
  Console.WriteLine("AfterInclStr = " + SourceURL.Substring(SURL,StrCut.AfterInclStr));
  Console.WriteLine("BeforeInclStr = " + SourceURL.Substring(SURL,StrCut.BeforeInclStr));
  Console.WriteLine("BeforeLessStr = " + SourceURL.Substring(SURL,StrCut.BeforeLessStr));
  Console.WriteLine("END");
 }
}

No comments:

Post a Comment