Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Tuesday, March 15, 2005

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)
Tuesday, March 15, 2005 6:55:35 PM (GMT Standard Time, UTC+00:00)  #    Comments [3] -
.NET
Tracked by:
"re:Sorting Datatables using DataView" (空调仪表) [Trackback]
Archive
<March 2005>
SunMonTueWedThuFriSat
272812345
6789101112
13141516171819
20212223242526
272829303112
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 260
This Year: 57
This Month: 0
This Week: 0
Comments: 116
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)