I have a string value of the form 10123X123456 , where 10 is the year, 123 is the number of the day in the year, and the rest is a unique system. In certain circumstances, I need to add 400 to the day number, so the number above, for example, becomes 10523X123456 .
My first idea was to fine-tune these three characters, convert them to an integer, add 400 to it, convert them back to a string, and then call replace in the original string. It works.
But then it occurred to me that the only character that I really needed to change was the third, and that the original value would always be 0-3, so there would never be any problems with hyphenation. It also occurred to me that the ASCII code for numbers is sequential, therefore, for example, adding the number 4 to the character โ0โ will result in โ4โ, etc. So what I finished doing.
My question is, is there some reason that will not always work? I usually avoid "ASCII arithmetic" on the grounds that it is not cross-platform or internationalist. But it seems reasonable to assume that the code points for numbers will always be sequential, i.e. "4" will always be 1 more than "3". Does anyone see any problems with this reasoning?
Here is the code.
string input = "10123X123456"; input[2] += 4;
source share