C# 如何从字符串中删除X个大写字母?
我想从C# 如何从字符串中删除X个大写字母?,c#,string,C#,String,我想从字符串中删除X个数的大写字母 例如,如果我有字符串: string Line1 = "NICEWEather"; 及 如何创建一个函数来提取两组大写字母?从字符串中删除大写字母 string str = " NICEWEather"; Regex pattern = new Regex("[^a-z]"); string result = pattern.Replace(str, ""); Console.WriteLine(result ); 输出:a
字符串中删除X
个数的大写字母
例如,如果我有字符串:
string Line1 = "NICEWEather";
及
如何创建一个函数来提取两组大写字母?从字符串中删除大写字母
string str = " NICEWEather";
Regex pattern = new Regex("[^a-z]");
string result = pattern.Replace(str, "");
Console.WriteLine(result );
输出:a或
若要删除大写字母,请按顺序多次出现,然后尝试此操作
string str = " NICEWEather";
Regex pattern = new Regex(@"\p{Lu}{2,}");
string output = pattern.Replace(str, "");
Console.WriteLine(output);
尝试对Unicode大写字母使用带\p{Lu}
的正则表达式
这里我们使用\p{Lu}{2,}
模式:大写字母出现X
(2
,在上面的代码中)或更多次。您尝试了什么吗?检查char值是否小于97@Z3RP你是说97?或者在65岁到65岁之间90@CoskunOzogul我认为StackOverflow社区的大多数人都会无缘无故地投反对票。@Rafalon,但这不是帮助新人的解决方案。Meh,为什么不匹配[a-Z]
(这里你删除了所有不是小写字母的东西)?OP还想删除连续的大写字母(因为它意味着什么)答案被更新。如果我们的连续大写字母数量不均匀怎么办?{2}
表示正好是2,所以很容易创建另一个反例:“ABCdef”
@DmitryBychenko,那么我们只需添加{2,}
string str = " NICEWEather";
Regex pattern = new Regex(@"\p{Lu}{2,}");
string output = pattern.Replace(str, "");
Console.WriteLine(output);
using System.Text.RegularExpressions;
...
// Let's remove 2 or more consequent capital letters
int X = 2;
// English and Russian
string source = "NICEWEather - ХОРОшая ПОГОда - Keep It (HAPpyhour)";
// ather - шая да - Keep It (pyhour)
string result = Regex.Replace(source, @"\p{Lu}{" + X.ToString() + ",}", "");