[英]RegEx to replace more than one hyphen with one hyphen in a string? asp.net C#
string inputString = "Flat---Head-----Self-Tap-Scr---ews----3-x-10mm-8pc";
string outputString = "Flat-Head-Self-Tap-Scr-ews-3-x-10mm-8pc";
string inputString = "Flat---Head-----Self-Tap-Scr---ews----3-x-10mm-8pc";
string outputString = Regex.Replace(inputString , @"-+", "-", RegexOptions.None);
正则表达式: -+
,替换为-
。 ;)
这是我的解决方案
text = "Flat---Head-----Self-Tap-Scr---ews----3-x-10mm-8pc";
while (text.Contains("--"))
{
text = text.Replace("--", "-");
}
您还可以使用Split
by -
并使用Join
text = "Flat---Head-----Self-Tap-Scr---ews----3-x-10mm-8pc";
string result = string.Join("-", text.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries));
第二个答案不是我自己的答案,我是从这个问题c# Trim commas until text appear 中得到的。 我想为您添加更多变量:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.