ATTENTION: I've decided to put the upgrade on hold due to a compatibility issue of our server environment with the latest CS installer package. CS 2008 now requires SQL Server 2005 as the backend DB but our database server currenlty has SQL Server 2000 installed on it. I'll resume the upgrade once I figure out when Telligent is releasing a patch to the schema compatibility issue. For now, we will continue to use the old version of CS while waiting for the said patch. If you have any questions about this process, please don't hesitate to post them on our forums and I'll answer them as soon as I can. Thanks for your patience and support guys! I'll let you know as soon as this is resolved. - Keith Rull

Left, Right and Mid functions in C#

I started as a VB programmer and I must say that i miss using Left, Right and Mid methods since it is not included in C#. But then again, there is always a suitable replacement. The Substring method.

The Substring method retrieves a substring from a specified string. In this demo i have decided to show how to use the substring method to create the Left, Right and Mid functions.

namespace LeftRightMid
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class LeftRightMid
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            
            //assign a value to our string
            string myString = "This is a string";
            //get 4 characters starting from the left
            Console.WriteLine(Left(myString,4));
            //get 6 characters starting from the right
            Console.WriteLine(Right(myString,6));
            //get 4 characters starting at index 5 of the string
            Console.WriteLine(Mid(myString,5,4));
            //get the characters from index 5 up to the end of the string
            Console.WriteLine(Mid(myString,5));
            //display the result to the screen
            Console.ReadLine();
        }

        public static string Left(string param, int length)
        {
            //we start at 0 since we want to get the characters starting from the
            //left and with the specified lenght and assign it to a variable
            string result = param.Substring(0, length);
            //return the result of the operation
            return result;
        }
        public static string Right(string param, int length)
        {
            //start at the index based on the lenght of the sting minus
            //the specified lenght and assign it a variable
            string result = param.Substring(param.Length - length, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param,int startIndex, int length)
        {
            //start at the specified index in the string ang get N number of
            //characters depending on the lenght and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param,int startIndex)
        {
            //start at the specified index and return all characters after it
            //and assign it to a variable
            string result = param.Substring(startIndex);
            //return the result of the operation
            return result;
        }

    }
}

Here is the result of the program.

 

Download the source code for this demo: LeftRightMid.zip (14.42 KB)

Posted Mar 15 2005, 06:55 PM by keithrull
Filed under:

Comments

vior wrote re: Left, Right and Mid functions in C#
on 08-26-2008 1:48 PM

I add some code to prevent errors while using variable input:

       public static string Mid(string param, int startIndex, int length)

       {

           string result = "";

           if (length <= 0) return "";

           //if (startIndex >= length) return "";

           try

           {

               //start at the specified index in the string ang get N number of

               //characters depending on the lenght and assign it to a variable

               result = param.Substring(startIndex, length);

               //return the result of the operation

               return result;

           }

           catch { }

           //the lenght is more than the margin

           //i.e. param =11 startIndex =6 lenght =10) --> margin that is left = 11-5 = 5

           try

           {

               result = param.Substring(startIndex, (param.Length - startIndex));

               return result;

           }

           catch { }

           return "";

       }

Eric wrote re: Left, Right and Mid functions in C#
on 10-01-2008 4:28 PM

Thank you very much.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:

Copyright DevPinoy 2005-2008