Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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.

Posted

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

Posted

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

Posted

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 ....

Dream as if you'll live forever, live as if you'll die today
Posted
I guess Regular expressions are cleaner becasue they were specifically made for this kind of issues ....
Dream as if you'll live forever, live as if you'll die today

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...