Getting x characters from the right side of a string

suggest change

Visual Basic has Left, Right, and Mid functions that return characters from the left, right, and middle of a string. C# does not have those methods. Substring can provide the same behavior as extension methods:

public static class StringExtensions
   {
      /// <summary>
      /// VB Left function
      /// </summary>
      /// <param name="stringparam"></param>
      /// <param name="numchars"></param>
      /// <returns>Left-most numchars characters</returns>
      public static string Left( this string stringparam, int numchars )
      {
         // A null string becomes empty. A non-string cannot be passed here.
         stringparam += string.Empty;
    
         // Handle possible negative numchars being passed
         numchars = Math.Abs( numchars );
    
         // Validate numchars parameter        
         if (numchars > stringparam.Length)
            numchars = stringparam.Length;
    
         return stringparam.Substring( 0, numchars );
      }
    
      /// <summary>
      /// VB Right function
      /// </summary>
      /// <param name="stringparam"></param>
      /// <param name="numchars"></param>
      /// <returns>Right-most numchars characters</returns>
      public static string Right( this string stringparam, int numchars )
      {
         // A null string becomes empty. A non-string cannot be passed here.
         stringparam += string.Empty;
    
         // Handle possible negative numchars being passed
         numchars = Math.Abs( numchars );
    
         // Validate numchars parameter        
         if (numchars > stringparam.Length)
            numchars = stringparam.Length;
    
         return stringparam.Substring( stringparam.Length - numchars );
      }
    
      /// <summary>
      /// VB Mid function - to end of string
      /// </summary>
      /// <param name="stringparam"></param>
      /// <param name="startIndex">VB-Style startindex, 1st char startindex = 1</param>
      /// <returns>Balance of string beginning at startindex character</returns>
      public static string Mid( this string stringparam, int startindex )
      {
         // A null string becomes empty. A non-string cannot be passed here.
         stringparam += string.Empty;
    
         // Handle possible negative startindex being passed
         startindex = Math.Abs( startindex );
    
         // VB's first character is 1. Past the end, Mid returns an empty string.
         if (startindex < 1 || startindex > stringparam.Length)
            return string.Empty;
         
         // C# strings are zero-based, convert passed startindex
         return stringparam.Substring( startindex - 1 );
      }
    
      /// <summary>
      /// VB Mid function - for number of characters
      /// </summary>
      /// <param name="stringparam"></param>
      /// <param name="startIndex">VB-Style startindex, 1st char startindex = 1</param>
      /// <param name="numchars">number of characters to return</param>
      /// <returns>Balance of string beginning at startindex character</returns>
      public static string Mid( this string stringparam, int startindex, int numchars)
      {
         // A null string becomes empty. A non-string cannot be passed here.
         stringparam += string.Empty;
    
         // Handle possible negative startindex being passed
         startindex = Math.Abs( startindex );
    
         // Handle possible negative numchars being passed
         numchars = Math.Abs( numchars );
    
         // VB's first character is 1. Past the end, Mid returns an empty string.
         if (startindex < 1 || startindex > stringparam.Length)
            return string.Empty;

         // C# strings are zero-based, convert passed startindex
         int start = startindex - 1;
         if (numchars > stringparam.Length - start)
            numchars = stringparam.Length - start;
         return stringparam.Substring( start, numchars );

       }
    }

This extension method can be used as follows:

string myLongString = "Hello World!";
string myShortString = myLongString.Right(6);  // "World!"
string myLeftString = myLongString.Left(5);    // "Hello"
string myMidString1 = myLongString.Mid(4);     // "lo World!"
string myMidString2 = myLongString.Mid(2, 3);   // "ell"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents