我想从X
中删除string
个大写字母。
例如,如果我有以下字符串:
string Line1 = "NICEWEather";
和
string Line2 = "HAPpyhour";
我将如何创建一个提取2套大写字母的函数?
答案 0 :(得分:1)
要从字符串中删除大写字母
string str = " NICEWEather";
Regex pattern = new Regex("[^a-z]");
string result = pattern.Replace(str, "");
Console.WriteLine(result );
输出:ather
如果顺序出现多次,请删除大写字母,然后尝试
string str = " NICEWEather";
Regex pattern = new Regex(@"\p{Lu}{2,}");
string output = pattern.Replace(str, "");
Console.WriteLine(output);
答案 1 :(得分:1)
尝试将带有\p{Lu}
的正则表达式用于Unicode 大写字母
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() + ",}", "");
这里我们使用\p{Lu}{2,}
模式:大写字母出现X
(在上面的代码中为2
)或多次。