C# 用一个字符替换所有出现的字符 [英] C# replace all occurrences of a character with just a character
本文介绍了C# 用一个字符替换所有出现的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个字符串,"%%%%%ABC"
,我想用一个 " 替换所有的
,所以应该是"%"
%""%ABC"
.
Let's say I have this string, "%%%%%ABC"
, I would like to replace all the "%"
with just one "%"
, so it should be "%ABC"
.
如果它的"%%%A%%B%%C%%"
,它应该是"%A%B%C%"
我该怎么做?
推荐答案
有一个使用 Regex.Replace
方法的解决方案:
There is a solution with Regex.Replace
method:
private static string TruncatePercents(string input)
{
return Regex.Replace(input, @"%+", "%");
}
这篇关于C# 用一个字符替换所有出现的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文