Friday, 19 February 2016

How to replace special character one by one from string?

    public static string ReplaceSpecialCharecters(string stringtoreplce, string charwithreplace)
        {
            string str = string.Empty;

            if (stringtoreplce.Contains(" "))
                stringtoreplce = stringtoreplce.Trim().Replace(" ", "-");

            if (stringtoreplce.Contains(enDash))
                stringtoreplce = stringtoreplce.Replace(enDash, '-');

            if (stringtoreplce.Contains('~'))
                stringtoreplce = stringtoreplce.Replace('~', '-');

            if (stringtoreplce.Contains('`'))
                stringtoreplce = stringtoreplce.Replace('`', '-');

            if (stringtoreplce.Contains('!'))
                stringtoreplce = stringtoreplce.Replace('!', '-');

            if (stringtoreplce.Contains('@'))
                stringtoreplce = stringtoreplce.Replace('@', '-');


            if (stringtoreplce.Contains('#'))
                stringtoreplce = stringtoreplce.Replace('#', '-');


            if (stringtoreplce.Contains('$'))
                stringtoreplce = stringtoreplce.Replace('$', '-');


            if (stringtoreplce.Contains('%'))
                stringtoreplce = stringtoreplce.Replace('%', '-');


            if (stringtoreplce.Contains('^'))
                stringtoreplce = stringtoreplce.Replace('^', '-');

            if (stringtoreplce.Contains('&'))
                stringtoreplce = stringtoreplce.Replace('&', '-');

            if (stringtoreplce.Contains('*'))
                stringtoreplce = stringtoreplce.Replace('*', '-');


            if (stringtoreplce.Contains('<'))
                stringtoreplce = stringtoreplce.Replace('<', '-');

            if (stringtoreplce.Contains('>'))
                stringtoreplce = stringtoreplce.Replace('>', '-');

            if (stringtoreplce.Contains(','))
                stringtoreplce = stringtoreplce.Replace(',', '-');

            if (stringtoreplce.Contains('/'))
                stringtoreplce = stringtoreplce.Replace('/', '-');


            if (stringtoreplce.Contains(';'))
                stringtoreplce = stringtoreplce.Replace(';', '-');

            if (stringtoreplce.Contains(':'))
                stringtoreplce = stringtoreplce.Replace(':', '-');

            if (stringtoreplce.Contains('|'))
                stringtoreplce = stringtoreplce.Replace('|', '-');

            if (stringtoreplce.Contains('\\'))
                stringtoreplce = stringtoreplce.Replace('\\', '-');


            if (stringtoreplce.Contains('"'))
                stringtoreplce = stringtoreplce.Replace('"', '-');

            if (stringtoreplce.Contains(rsingleQuote))
                stringtoreplce = stringtoreplce.Replace(rsingleQuote, '-');

            if (stringtoreplce.Contains('.'))
                stringtoreplce = stringtoreplce.Replace('.', '-');

            while (stringtoreplce.Contains("--"))
                stringtoreplce = stringtoreplce.Replace("--", "-");

            return stringtoreplce;
        }

No comments:

Post a Comment