Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store the number from below string into another string.
'targetString should be "3577610"

My code is:

Dim sourceString as string="Cardrew Industrial.3577610 (kWh)"
Dim targetString as string = sourceString.Substring(sourceString.IndexOf(".") + 1, sourceString.length-6)

But it is throwing an error message.

Any suggestions please?
Posted
Comments
[no name] 28-Jun-11 11:28am    
Any suggestions please? Yes, tell us what the exception is.
Member 8001800 28-Jun-11 11:37am    
Argument out of range exception was unhandled.
"Index and length must refer to a location within the string. Parameter name: length"

VB
Dim sourceString As String = "Cardrew Industrial.3577610 (kWh)"
Dim Index As Integer = sourceString.IndexOf(".") + 1
Dim IndexEnd As Integer = sourceString.LastIndexOf(" ")
Dim Length As Integer = sourceString.Length - IndexEnd
Dim targetString As String = sourceString.Substring(Index, Length)


The second argument is number of characters in the string that you want, not the end position of the string.
 
Share this answer
 
Comments
Abhinav S 28-Jun-11 13:18pm    
Precise. 5.
You miscalculated the second parameter of string.Substring. It is, logically, not position but length. Should be:

VB
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
 
Share this answer
 
Comments
Abhinav S 28-Jun-11 13:18pm    
Good stuff. 5.
Sergey Alexandrovich Kryukov 28-Jun-11 13:28pm    
Thank you, Abhinav.
--SA
If it is a pattern that every time you want to read number value then you can use regular expression as well.
 
Share this answer
 
Comments
Member 8001800 28-Jun-11 12:28pm    
The pattern will be the same like name.id (units) but the problem is sometimes id will be mixed case(ex:f786345). In this case which will be the best way? Hard coding or regex?
Abhinav S 28-Jun-11 13:18pm    
Yup. Regular expressions is a good idea. 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900