C# 字符串中所有字母字符的大小写反转
反转 C# 字符串中所有字母字符大小写的最简单方法是什么?例如“aBc1$;”应变为“AbC1$;”我可以轻松地编写一个方法来执行此操作,但我希望有一个我不知道的库调用会让这变得更容易。我还想避免列出所有已知的字母字符并将每个字符与列表中的字符进行比较。也许这可以用正则表达式来完成,但我不太了解它们。谢谢。
感谢您的帮助。我为此创建了一个字符串扩展方法,该方法主要受到 Anthony Pegram 解决方案的启发,但没有 LINQ。我认为这在可读性和性能之间取得了很好的平衡。这是我想出的。
public static string SwapCase(this string source) {
char[] caseSwappedChars = new char[source.Length];
for(int i = 0; i < caseSwappedChars.Length; i++) {
char c = source[i];
if(char.IsLetter(c)) {
caseSwappedChars[i] =
char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c);
} else {
caseSwappedChars[i] = c;
}
}
return new string(caseSwappedChars);
}
What is the simplest way to reverse the case of all alphabetic characters in a C# string? For example "aBc1$;" should become "AbC1$;" I could easily write a method that does this, but I am hoping there is a library call that I don't know about that would make this easier. I would also like to avoid having a list of all known alphabetic characters and comparing each character to what is in the list. Maybe this can be done with regular expressions, but I don't know them very well. Thanks.
Thanks for the help. I created a string extension method for this that is mostly inspired by Anthony Pegram's solution, but without the LINQ. I think this strikes a good balance between readability and performance. Here is what I came up with.
public static string SwapCase(this string source) {
char[] caseSwappedChars = new char[source.Length];
for(int i = 0; i < caseSwappedChars.Length; i++) {
char c = source[i];
if(char.IsLetter(c)) {
caseSwappedChars[i] =
char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c);
} else {
caseSwappedChars[i] = c;
}
}
return new string(caseSwappedChars);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用 LINQ 来完成此操作。一种方法:
You could do it in a line with LINQ. One method:
如果您不关心国际化:
另一方面,如果您确实关心国际化,则需要将
CultureInfo.InvariantCulture
传递给 ToUpper() 和 ToLower() 函数。 ..If you don't care about internationalization:
If, on the other hand, you DO care about internationalization, you'll want to pass in
CultureInfo.InvariantCulture
to your ToUpper() and ToLower() functions...如果您不了解 LINQ,您可以采用老式方法。
You could do it old-school if you don't know LINQ.
以下是正则表达式方法:
您可以使用
Char.Parse(m.Value)
作为m.Value[0]
的替代方案。另外,请注意改用ToUpperInvariant
和ToLowerInvariant
方法。有关详细信息,请参阅此问题: 在 C# 中,什么是ToUpper() 和 ToUpperInvariant() 之间的区别?Here's a regex approach:
You can use
Char.Parse(m.Value)
as an alternate tom.Value[0]
. Also, be mindful of using theToUpperInvariant
andToLowerInvariant
methods instead. For more info see this question: In C# what is the difference between ToUpper() and ToUpperInvariant()?我为字符串创建了一个扩展方法,它就是这样做的!
使用
I made an extension method for strings which does just this!
To use
昨天有人问我类似的问题,我的回答是:
您可以轻松更改方法签名以添加
CultureInfo
类型的参数,并将其与char.ToUpper
等方法一起使用> 满足全球化的要求。I was asked a similar question yesterday and my answer is like:
You can easily change the method signature to add a parameter of type
CultureInfo
, and use it with methods likechar.ToUpper
for a requirement of globalization.比这里列出的其他一些方法快一点,它很好,因为它使用 Char 算术!
A little bit faster than some other methods listed here and it is nice because it uses Char arithmetics!
这将对您有更多帮助..因为在这里我没有直接使用函数。
我相信这也适合你..谢谢。
This will helps you more.. because here i have not use directly function.
I am sure this will also works for you.. Thank you.
下面的代码仅对每个字母进行 2 次函数调用。我们不检查 IsLetter 是否存在,而是在必要时应用大写/小写。
也可以(尽管可读性较差)创建一个额外的变量,并在检查之前将其设置为 char.ToLower(item),将一个函数调用交换为一个额外的变量,这样:
Code below makes only 2 function calls to each letter. Instead of checking if IsLetter, we just apply upper/lowercase if necessary.
It would be also possible (tho less readable) to create an extra variable and set it to char.ToLower(item) before the check, exchanging one function call for one extra variable, thie way: