String Manipulation

Roey

Well-known member
Joined
Oct 10, 2002
Messages
238
Location
Canada
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
 
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.
 
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
 
I did it using Regular Expressions which Im 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
 
If you are sure your strings always have the same structure

You can use
[VB]
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))
[/VB]

This may not b the cleanest but it works ....
 
I guess Regular expressions are cleaner becasue they were specifically made for this kind of issues ....
 
Back
Top