I am importing some number of records with multiple string fields from an old db to a new db. It seems to be very slow and I suspect it's because I do this:
foreach (var oldObj in oldDB)
{
NewObject newObj = new NewObject();
newObj.Name = oldObj.Name.Trim().Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š')
.Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć')
.Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
newObj.Surname = oldObj.Surname.Trim().Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š')
.Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć')
.Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
newObj.Address = oldObj.Address.Trim().Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š')
.Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć')
.Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
newObj.Note = oldObj.Note.Trim().Replace('^', 'Č').Replace('@', 'Ž').Replace('[', 'Š')
.Replace(']', 'Ć').Replace('`', 'ž').Replace('}', 'ć')
.Replace('~', 'č').Replace('{', 'š').Replace('\\', 'Đ');
/*
... some processing ...
*/
}
Now, I have read some posts and articles through the Net where I have seen many different thoughts about this. Some say it's better if I'd do regex with MatchEvaluator, some say it's the best to leave it as is.
While it's possible that it'd be easier for me to just do a benchmark case for myself, I decided to ask a question here in case someone else has been wondering about the same question, or in case someone knows in advance.
So what is the fastest way to do this in C#?
EDIT
I have posted the benchmark here. At the first sight it looks like Richard's way might be the fastest. However, his way, nor Marc's, would do anything because of the wrong Regex pattern. After correcting the pattern from
@"\^@\[\]`\}~\{\\"
to
@"\^|@|\[|\]|`|\}|~|\{|\\"
it appears as if the old way with chained .Replace() calls is the fastest after all