Friday, May 31, 2019

C-Sharp : A better Substring extension for common uses

Here's a better Substring extension for common use patterns.

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 First/LastPosition, 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;

public static class Program
{
     /// <summary>
        /// Define common patterns for substring actions
        /// </summary>
        public enum StrCut
        {
            BeforeLessStr = 0,
            BeforeInclStr = 1,
            AfterLessStr = 2,
            AfterInclStr = 4
        }
        /// <summary>
        /// Cut from front or back
        /// </summary>
        public enum StrCutLoc
        {
            First = 0,
            Last = 1
        }
        /// <summary>
        /// Extend Susbstring with ability to pass a string and choose common actions surrounding that string.
        /// </summary>
        /// <param name="s">haystack</param>
        /// <param name="arg">needle</param>
        /// <param name="l">search location</param>
        /// <param name="e">include needle</param>
        /// <returns></returns>
        /// <author>//Fri 31-May-19 12:45pm metadataconsult@gmail.com</author>
        public static string Substring(this string s, string arg, StrCutLoc l, 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 = -1; 

            if (l.Equals(StrCutLoc.First))
                idxarg = s.IndexOf(arg);
            else if (l.Equals(StrCutLoc.Last))
                idxarg = s.LastIndexOf(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 text = "{\"InsertSymbolText\",\"InsertSymbol\"},";  
  string cut = ",";
 
  Console.WriteLine("text = "+ text); 
  Console.WriteLine();
  Console.WriteLine("Let's get rid of length param, like this Substring(2,2) = " + text.Substring(2,2)); 
  Console.WriteLine();
 
  Console.WriteLine("BEGIN - Most of time, I just want to get a supply a string a work around it"); 
  
  Console.WriteLine("AfterLessStr = " + text.Substring(cut, StrCutLoc.First, StrCut.AfterLessStr));
  Console.WriteLine("AfterInclStr = " + text.Substring(cut, StrCutLoc.First, StrCut.AfterInclStr));
  Console.WriteLine("BeforeInclStr = " + text.Substring(cut, StrCutLoc.Last, StrCut.BeforeInclStr));
  Console.WriteLine("BeforeLessStr = " + text.Substring(cut, StrCutLoc.Last, StrCut.BeforeLessStr));
  Console.WriteLine("END");
 }
}

No comments:

Post a Comment