Remove first and last special characters from string in c#

Sometimes we need to remove special character from staring and ending of a string.

private static string RemoveFirstAndLastSpecialCharacter(string input)
{
    if (input != string.Empty)
    {
        string firstRemovableStr = string.Empty;
        string lastRemovableStr = string.Empty;
        bool IsLetter = false;
        for (int i = 0; i < input.Length; i++)
        {
            if (!char.IsLetter(input[i]) && !IsLetter)
                IsLetter = false;
            else
                IsLetter = true;

            if (IsLetter)
                firstRemovableStr = firstRemovableStr + input[i];
        }
        input = firstRemovableStr;
        IsLetter = false;
        for (int k = 0; k < input.Length; k++)
        {
            if (!char.IsLetter(input[(input.Length - k) - 1]) && !IsLetter)
                IsLetter = false;
            else
                IsLetter = true;

            if (IsLetter)
                lastRemovableStr = input[(input.Length - k) - 1] + lastRemovableStr;
        }
        input = lastRemovableStr;
    }
    return input;
}


Comments

Popular posts from this blog

Add Image through base64 on PDF in itextsharp

how to insert text and tags at the cursor CKEDITOR