开发者

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 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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜