Friday, September 3, 2010

Excel Functions - Left Trim, Mid Trim, Right Trim

LeftTrim function extracts a substring from a string, starting from the left-most character


private string LeftTrim(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;
        }


RightTrim function extracts a substring from a string starting from the right-most character


private string RightTrim(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;
        }


MidTrim function extracts a substring from a string (starting at any position)


 private string MidTrim(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;
        }

No comments:

Post a Comment