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]
Tuesday, March 15, 2005 8:58:35 PM (GMT Standard Time, UTC+00:00)
Keith:

No argument-validity checking is done ... so it would be easy to crash it by passing zero length or null strings or negative position/length !

- Adwait
Tuesday, March 15, 2005 9:05:29 PM (GMT Standard Time, UTC+00:00)
Adwait Ullal:

You are right about your analogy about the program. This sample program doesn't handle tgose kind of stuff since its main purpose is to show how to accomplish Left, Right and Mid methods in C# using the Substring method. You can extend this sample by adding error handlers that would trap zero lenght, null string and negative index positions.
Thursday, June 28, 2007 6:43:07 PM (GMT Standard Time, UTC+00:00)
Then again.. you could always use these functions from C# by typing in Microsoft.VisualBasic.Strings.Left(...)
;)

Comments are closed.
On this page
Archive
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
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)