c# - How to replace the special character and space with particular symbol in Regex? -
i'm developing windows phone 8 application.
i need replace special character,space,and multiple space - [dash symbol]
sample
with out replace string - hampton inn & suites tampa east (casino area)
i need - hampton-inn-suites-tampa-east-casino-area
i tried regex this
c#
string passvalue = regex.replace(name, @"[ '&().,/]", "-");
above code output
hampton-inn---suites-tampa-east--casino-area-
but need hampton-inn-suites-tampa-east-casino-area
i need remove
1.(inn---suites) (inn-suites)
2.(--casino-area) (-casino-area)
3.i don't need - @ last (area-) (area)
thank you
first add +
quantifier after character class []
remove multiple occurrence. after trim
hyphen remove leading or trailing.
string passvalue = regex.replace(name, @"[ '&().,/]+", "-").trim('-'); ^ added
but, i'll suggest use regex [^a-za-z]+
case:
string passvalue = regex.replace(name, @"[^a-za-z]+", "-").trim('-');
Comments
Post a Comment