Dmcovey1993
Dmcovey1993

Reputation: 75

vb.net Manipulation of a string

Hello I am trying to get part of my string out and was wondering if anyone can help so here is what I am working with

Dim tempName, NewName As String

'This is an example of what tempName will Equal
'What I need is the information in between the first - 
'and the second one.  In this case it would be 120
'I can not do it by number because the dashes are the only thing
'that stays the same.
tempName = "3-120-12-6"

NewName = tempName 'Do not know what String Manipulation to use.

Another few examples would be 6-56.5-12-12 I need the 56.5 or 2-89-12-4 I would need the 89 Thank you for the help.

Upvotes: 0

Views: 120

Answers (1)

chris_techno25
chris_techno25

Reputation: 2487

You can do this too... You can break it up into pieces by detecting the first -. Then get the second...

Dim x as String = Mid(tempName, InStr(tempName, "-") + 1)
NewName = Mid(x, 1, InStr(x , "-") - 1)

Or make something like this to get it into 1 line of code...

NewName = Mid(Mid(tempName, InStr(tempName, "-") + 1), 1, InStr(Mid(tempName, InStr(tempName, "-") + 1), "-") - 1)

Or the quickest approach to this would be to use Split like the code below... This code equates the values between - into an array. Since you want the second value, you'd want the array with the index of 1 since an array starts with 0. If you're only starting, do try to create your own way to manipulate the string, this will help your mind think of new things and not just rely on built-in functions...

Dim tempName As String = "3-120-12-6"
Dim secondname() As String = Split(tempName, "-")
NewName = secondname(1)

Upvotes: 2

Related Questions