tags:

views:

48

answers:

3

Hi..all

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; }

+2  A: 

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

A_Nablsi
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.
Steve Haigh
I did the same..but it is not converting the string to uppercase when it has special characters like '@'
karthik
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.
A_Nablsi
A: 

if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '\')Firstly, string.ToUpper() gets your upper case characters.

To get rid of all special characters you would either use a regex or loop through the string and copy 'A-Z' and '/' in to a new string. This filters out all other characters without needing to know what they are.

E.g.

        string invalid = @"as@bc3423*%*%ihh";
        string upper = invalid.ToUpper();
        string result = string.Empty;

        foreach (char c in upper)
        {
            if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '\\')                    
            result += c;
        }

        Console.WriteLine(result);
Steve Haigh
+3  A: 

Use the following Regex replacement:

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

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

Kamyar
Yeah, I prefer regex to my loop version actually:-)
Steve Haigh
@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.
Kamyar
karthik
@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.
Steve Haigh
Ya..i did it...it is wokring fine...thanks for the solutions guys..
karthik
@Steve: Thanks. Wasn't at my desk for a few minutes.
Kamyar