Friday, September 3, 2010

Sub-String a String from End

Have you come across a scenario where you need to sub-string a string from the end, where you know a defined set of characters to be removed. Following is the snippet explains you how


private string removeFromEnd(string source, string subString)
        {
            string retVal = string.Empty;
            int sourceLength = source.Length;
            int subStringLength = subString.Length;
            int pos = sourceLength - subStringLength;
            retVal = source.Substring(0, pos);


            return retVal;
        }

No comments:

Post a Comment