Roey Posted December 9, 2003 Posted December 9, 2003 I have a string eg: ABC123 - This is a component (4 per assembly) and I need to extract the # per assembly in this case 4. The # per assembly could be from anywhere between 0.0001 and 10000 Thanks Quote
*Experts* Volte Posted December 9, 2003 *Experts* Posted December 9, 2003 You should look into using regular expressions for parsing strings. System.Text.RegularExpressions. Alternatively, you can use the StringVar.IndexOf() method to find the position of the first bracket before the number and the space afterwards, and StringVar.SubString() to extract the correct number of characters. Quote
Engine252 Posted December 9, 2003 Posted December 9, 2003 do you want to remove the or know how much # are in the string or get the # in a sepparate string?? for removal you could use string.replace("#","") for the number of # you could use string.contains("#") or instr("stre,...") and for the last you can use string.split and the reading each string in the array not sure wath you mean?? hope this can help you if not let me know Quote
Roey Posted December 9, 2003 Author Posted December 9, 2003 I did it using Regular Expressions which I'm brand new to. Any comments or suggestions would be gladly welcomed: Dim s As String = "ABC123 - This is a component (4 per assembly)" Dim x As String Dim re As New System.Text.RegularExpressions.Regex("\(\d+") Dim m As System.Text.RegularExpressions.Match m = re.Match(s) x = m.ToString MessageBox.Show (x.Remove(0, 1)) The result is the number 4 Quote
Mehyar Posted December 10, 2003 Posted December 10, 2003 If you are sure your strings always have the same structure You can use Dim index as integer = S.IndexOf("(") s = s.Substring(index + 1) 'now you have "4 per assembly)" Dim Arr() as String = s.Split(" ") 'split the sentence by space 'Arr(0) will contain your first number Dim MyNumber as Integer = Convert.ToDouble(Arr(0)) This may not b the cleanest but it works .... Quote Dream as if you'll live forever, live as if you'll die today
Roey Posted December 10, 2003 Author Posted December 10, 2003 WHich would be the better of these two methods ? Quote
Mehyar Posted December 11, 2003 Posted December 11, 2003 I guess Regular expressions are cleaner becasue they were specifically made for this kind of issues .... Quote Dream as if you'll live forever, live as if you'll die today
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.