Search This Blog

Friday, October 16, 2015

VB6 string Left, Right and Mid functions in C#

While working on migration code from VB6 to C#, we encounter that the in-build string functions which were available in VB6 but are not available in C#. For ex – Left, Mid and Right functions.
One possible solution is to use Substring method however it has its own caveat. Substring method works until the provided string has a known length; and It throws exception if the length is unknown (while VB6 does not raise any exception).

you can use the below string extensions method to achieve VB equivalent functionality:
    public static class StringExtensions
    {
        /// <summary>
        /// Retrieves a substring from this instance. The substring starts at a specified length from right.
        /// </summary>
        /// <param name="self">The self instance.</param>
        /// <param name="length">The number of characters in the substring.</param>
        /// <returns>
        /// The substring starts at a specified length from right.
        /// </returns>
        public static string Right(this string self, int length)
        {
            if (string.IsNullOrEmpty(self))
            {
                return string.Empty;
            }
            else if (length >= self.Length)
            {
                return self;
            }
            else
            {
                return self.Substring(self.Length - length);
            }
        }

        /// <summary>
        /// Retrieves a substring from this instance. The substring starts at a specified length from left.
        /// </summary>
        /// <param name="self">The self instance.</param>
        /// <param name="length">The number of characters in the substring.</param>
        /// <returns>
        /// The substring starts at a specified length from left
        /// </returns>
        public static string Left(this string self, int length)
        {
            if (string.IsNullOrEmpty(self))
            {
                return string.Empty;
            }
            else if (length >= self.Length)
            {
                return self;
            }
            else
            {
                return self.Substring(0, length);
            }
        }

        /// <summary>
        /// Retrieves a substring from this instance. The substring starts at a specified position.
        /// </summary>
        /// <param name="self">The self instance.</param>
        /// <param name="start">The zero-based starting character position of a substring in this instance.</param>
        /// <returns>
        /// The substring starts at a specified position to length of instance.
        /// </returns>
        public static string Mid(this string self, int start)
        {
            if (string.IsNullOrEmpty(self))
            {
                return string.Empty;
            }

            return Mid(self, start, self.Length);
        }

        /// <summary>
        /// Retrieves a substring from this instance. The substring starts at a specified position to provided length.
        /// </summary>
        /// <param name="self">The self instance.</param>
        /// <param name="start">The zero-based starting character position of a substring in this instance.</param>
        /// <param name="length">The number of characters in the substring.</param>
        /// <returns>
        /// The substring starts at a specified position to length of instance to provided length.
        /// </returns>
        public static string Mid(this string self, int start, int length)
        {
            if (string.IsNullOrEmpty(self))
            {
                return string.Empty;
            }
            else if (start > self.Length)
            {
                return string.Empty;
            }
            else if (length >= (self.Length - start))
            {
                // if no. of characters requested for is more than length available
                return self.Substring(start);
            }
            else
            {
                return self.Substring(start, length);
            }
        }
    }

You can read about other equivalent methods at here.

No comments:

Post a Comment