string inputString = "Flat---Head-----Self-Tap-Scr---ews----3-x-10mm-8pc";
string ou开发者_开发问答tputString = "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);
Regex: -+
, replace with -
. ;)
Here's my solution
text = "Flat---Head-----Self-Tap-Scr---ews----3-x-10mm-8pc";
while (text.Contains("--"))
{
text = text.Replace("--", "-");
}
You can also use Split
by-
and use Join
text = "Flat---Head-----Self-Tap-Scr---ews----3-x-10mm-8pc";
string result = string.Join("-", text.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries));
The second answer isn't my own answer, I got it from this question c# Trim commas until text appears. I wanted to add you more variables :)
精彩评论