使用 C# 将字符串从小写转换为大写并从字符串中删除特殊字符
我正在开发一个带有用户 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用以下正则表达式替换:
输入:
inputStr=@"xya\t4z567"
输出:“XYA\T4Z567”
Use the following Regex replacement:
Input:
inputStr=@"xya\t4z567"
output:
"XYA\T4Z567"
使用“yourstring”.ToUpper()
您可以使用 "yourstring".Replace() 删除特殊字符。
Use "yourstring".ToUpper()
and you can use "yourstring".Replace() to remove the special characters.
if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' \')首先, string.ToUpper() 获取大写字符。
要摆脱所有特殊字符,您可以使用正则表达式或循环遍历字符串并将“AZ”和“/”复制到新字符串中。这会过滤掉所有其他字符,而无需知道它们是什么。
例如
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.