The question cites Regex and the accepted answer gave a good solution. I prefer to do it manually because I make it easier than I would with Regex, but I know that’s not the case with everyone. If performance is important Regex is not always a good option. Almost all algorithms can be done faster if produced manually. That’s what I did:
public static string MudaEndereco(string texto, char adicao = '~') {
var resultado = new StringBuilder(texto.Length * 2);
var anterior = '\0';
foreach (var caractere in texto) {
if (Char.IsDigit(anterior) && Char.IsWhiteSpace(caractere)) {
resultado.Append(adicao);
}
resultado.Append(caractere);
anterior = caractere;
}
return resultado.ToString();
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Regex lost to the manual algorithm on average by at least 4X. There were dozens of cases I disregarded, perhaps because of garbage collection. 4X is no small thing. Depending on the machine the difference was in 6X or more. I do not understand so much of Regex, I may have done something wrong, but I did on top of what was answered. I tried some optimizations that the Regex’s. Net allows and only worsened :).
I even used a criterion that may have slowed down because I didn’t just take a white character, I took any character that is considered a white one, if the requirement doesn’t allow it is just switch to a simple one ' '
. The check of digits is also done so that it works where the numbers are represented in an unusual way in the Unicode table, this certainly also slows down.
I made an extra example more customizable and the result was pretty much the same.
Note that I have optimized the Regex standard, the ?
made no sense there and could use \d
. Then I would:
Regex.Replace(lista, "(\\d+) ", "$1~ ")
So if the answer had to be Regex this would be my code.
This should also work: http://ideone.com/59v7or
– stderr
@stderr, thanks also work.
– Marco Souza