How to remove more than one dot (.) from text?
How to remove more than one dot (.) from text ?
for example:
123..45 =开发者_Go百科 123.45
10.20.30 = 10.2030
12.34 = 12.34
12.. = 12
123...45 = 123.45
how to do it ?
thanks in advance
It is not necessary to use regex, you can achieve what you need in this way:
string s = "10.20.30";
int n;
if( (n=s.IndexOf('.')) != -1 )
s = string.Concat(s.Substring(0,n+1),s.Substring(n+1).Replace(".",""));
Regex.Replace("Yourstring", "[.]{2,}",".");
Ok, using this overload of Regex.Replace
, the trick here is to match everything after the first .
, by using a look-behind group [ (?<=
] then to use a MatchEvaluator
to clean that Match
.
Regex.Replace(
"127.0.8", //input string
@"(?<=\.).*", //match everything after 1st "."
m => m.Value.Replace(".",string.Empty)) //return value of match without "."
using System.Linq;
...
string s = "10.20.30";
while (s.Count( c => c == '.') > 1)
s = s.Remove( s.LastIndexOf('.'),1);
精彩评论