I have a string where the third last character is sometimes a ,
If this is the case I want to replace it with a .
The string could also have other ,
's throughout. Is there an elegant solution to this?
EDIT: Thanks everyone for your answers. Just to clarify, yes by third last I mean a string of the form xxxxxx,xx
(it's a european currency thing)
Try this
It should do it if by 'third last character' you literally mean the third-last character in the whole string.
That said - you might need to tweak if there are new lines - e.g. add the
RegexOptions.Singleline
enum as an extra parameter.For better performance - probably - you could pre-declare the regex inside a class body:
Then when you want to use it it's just:
A more proper method would probably be to use the cultures
How about:
EDIT: I hope the above is just about the most efficient approach. You could try using a char array instead:
Using
Substring
will work as well, but I don't think it's any more readable:EDIT: I've been assuming that in this situation you already know that text will be at least three characters length. If that's not the case, you'd obviously want a test for that as well.