使用 C# 将字符串从小写转换为大写并从字符串中删除特殊字符

发布于 2024-09-29 02:53:46 字数 775 浏览 5 评论 0原文

我正在开发一个带有用户 ID 的登录表单。我希望用户以指定的格式创建用户ID。我需要一种使用 C# 将所有小写字母转换为大写字母的方法。用户 ID 将采用以下格式。 格式为:

xyz\t4z4567(字符不区分大小写)

规则:

1.创建用户名时只允许使用特殊字符\。 2.UserID应转换为大写(xyz --> XYZ) 我需要检查用户在创建用户 ID 时是否输入了任何特殊字符。如果 UserID 中有任何特殊字符,该方法需要删除特殊字符,并将所有小写字母转换为大写字母。

最后结果应如下所示:

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

我使用以下方法来检查字符串是否包含以下字符,如果是,我将替换为空。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪之魂 2024-10-06 02:53:46

使用以下正则表达式替换:

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

输入:inputStr=@"xya\t4z567"
输出:“XYA\T4Z567”

Use the following Regex replacement:

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

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

彼岸花ソ最美的依靠 2024-10-06 02:53:46

使用“yourstring”.ToUpper()
您可以使用 "yourstring".Replace() 删除特殊字符。

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

橘寄 2024-10-06 02:53:46

if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' \')首先, string.ToUpper() 获取大写字符。

要摆脱所有特殊字符,您可以使用正则表达式或循环遍历字符串并将“AZ”和“/”复制到新字符串中。这会过滤掉所有其他字符,而无需知道它们是什么。

例如

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文