How to replace five digits from a string? Possibly a solution in Regexes, my regular expression skills are not so strong.
string ort = "42671 VÄSTRA FRÖLUNDA";
How to replace five digits from a string? Possibly a solution in Regexes, my regular expression skills are not so strong.
string ort = "42671 VÄSTRA FRÖLUNDA";
\d{5}
will match five digits anywhere in a string. This can be used with Regex.Replace
.
String result = Regex.Replace("input string",@"\d{5}",ReplaceFiveDigits);
private static string ReplaceFiveDigits(Match m)
{
return "VALUE TO REPLACE";
}
var replaced = Regex.Replace(ort, @"\d{5}", "REPLACE WITH THIS");
will replace any 5 consecutive digits.
do you also want to remove the space afterwards?
var replaced = Regex.Replace(ort, @"\d{5}\s?", "REPLACE WITH THIS");