Convert string from lowercase to uppercase and removing special charaters from a string using C#

10,016

Solution 1

Use the following Regex replacement:

outputStr = System.Text.RegularExpressions.Regex.Replace(inputStr, @"[^a-zA-Z0-9_\\]", "").ToUpperInvariant();  

Input: inputStr=@"xya\t4z567"
output: "XYA\T4Z567"

Solution 2

Use "yourstring".ToUpper() and you can use "yourstring".Replace() to remove the special characters.

Share:
10,016
karthik k
Author by

karthik k

Updated on June 12, 2022

Comments

  • karthik k
    karthik k almost 2 years

    I am developing a login form with User ID. I want the user to create the userid in a specified format. I need a method using C# to convert all lowercase letters to uppercase. The userid will be in the following fomat. The format is:

    xyz\t4z4567 (characters are not case sensitive)

    Rules:

    1.Only special character \ is allowed while creating user name. 2.The UserID sholud be converted to uppercase.like (xyz --> XYZ) I need to check if user enters any special characters while creating the userid. If any special charcters are there in UserID the method needshold remove the special characters and should convert all lowercase to uppercase lettrs.

    finally the result should be in the following way :

    xyz\t4z45@67 ---> XYZ\T4Z4567

    I used the following method to check whether the string contains the following characters and if so i am replacing with empty.

    public string RemoveSpecialChars(string str)
    {
       string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", ";", "-", "_", "(", ")", ":", "|", "[", "]" };
    
        for (int i = 0; i < chars.Length; i++)
        {
            if (str.Contains(chars[i]))
            {
                str = str.Replace(chars[i], "");
            }
        }
    
        return str;
    }
    
  • Steve
    Steve over 13 years
    string.Replace is fine, but you'd need to know all the characters you want to get rid of, and there could be a lot (the question doesn't specify), or you may not even know what they are.
  • karthik k
    karthik k over 13 years
    I did the same..but it is not converting the string to uppercase when it has special characters like '@'
  • Steve
    Steve over 13 years
    Yeah, I prefer regex to my loop version actually:-)
  • Kamyar
    Kamyar over 13 years
    @Steve: Using regex, reduces your code length significantly. Very effective and useful. But make sure to leave some comments if your code is going to be used and studied by others. They're a pain in the neck when you want to find out what they exactly do.
  • A_Nabelsi
    A_Nabelsi over 13 years
    The ToUpper method do convert characters to uppercase even if there are special characters in the string, make sure you understand that the ToUpper does not convert the original string but it returns a new string with characters converted to uppercase.
  • karthik k
    karthik k over 13 years
    Hi kamyar..thanks for the solution. It is working fine . And i faced one issue while using thi expression. I gave the input as MSI+_M)S@I!1#2&7)& and it is returning the string as MSI\_MSI127 . Please correct me where am i missing in the expression.
  • Steve
    Steve over 13 years
    @karthik I think you need to make some attempt to understand Kamyar's regex:-). The fix is very simple, just remove the '_' from his regex.
  • Kamyar
    Kamyar over 13 years
    @Steve: Thanks. Wasn't at my desk for a few minutes.