You miscalculated the second parameter of
string.Substring
. It is, logically, not position but length. Should be:
Dim targetString as string =
sourceString.Substring(sourceString.IndexOf(".") + 1, 7)
However,
the whole approach is wrong. Using
immediate constants (hard-coded) like 6 or 7 will not lead you anywhere. You should generally avoid any
immediate constants. They have to go to as set of explicit constants, resources or variables stored in configuration file(s). If you hard-code, how can you support this code? But in this case, any constants for length, position and the like makes no sense.
You could use Regular Expressions with the pattern like "\.[0-9]*" or "[0-9]*" to extract the match. See
System.Text.RegularExpressions.Regex
,
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[
^].
By even if you did it properly, it would be wrong approach. You should not avoid storing numeric in string. Try to do just the opposite. Work with numeric types and generate strings out of it only when you need to present data in UI. Use
System.String.Format
,
http://msdn.microsoft.com/en-us/library/system.string.aspx[
^]. Well, if possible.
—SA