Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string

Status
Not open for further replies.

aajay

Technical User
Oct 28, 2004
36
Hi
I have string M2549 - 2598
like to get M2549
I'm new to vb.net
I try split function but couldn't get it
any would be appreciated

Thanks
 
Hi
I didn't explain my question very well

I like to assign split value to a variable

dim mystring = split("M2549 - 2598","-")

and get
mystring = M2549


Thanks
 
try this:

Code:
'If it's really only "M2549" you're interested in
Dim myString as String = String.Split("M2549 - 2598","-").getvalue(0)

Hope it suits your needs.

---
If gray hair is a sign of wisdom, then talk like a pirate ninja monkey if you get the time to get funky once a week.
Wo bu zhi dao !
 
Code:
Dim startstring as string = "M2549 - 2598"
Dim strsplit as string = "-"

Dim strResult As String = ""

If startstring.contains(strsplit) then
   strresult = Mid(startstring, 1, InStr(startstring, strsplit) - 1)
end if
 
I'm trying to get
mystring = "M2549 - 2598"

mystring = Split("M2549 - 2598","-")


and would like to get

mystring = M2549



 
Code:
Dim myString as string = "M2549 - 2598"
Dim strsplit as string = "-"

If mystring.contains(strsplit) then
   mystring = Mid(mystring, 1, InStr(mystring, strsplit) - 1)
end if
[code]
 
Is the above a problem with the server or is it coffee shake on the enter key :-D

oh, no I now see the late no explanation changes to your post(s).

Just to add my bit (after that), 'Split("M2549 - 2598","-")(0)' will fix your problem. The split function returns an array of string, so you will need to select the one you want '(0)'. You may want to add '.Trim' to the end of the function to remove the space between the first section and the '-'. This would then make your code:
Code:
mystring = Split("M2549 - 2598","-")(0).Trim
 
It's funny that nobody has yet proposed a Regular expression to solve this. If you check on the others threads, you'll notice that there's allways someone to say : "This is typically a regex job" or "Why not simply use a simple regex?". So this time I'll have the honor to propose a regex solution:

Code:
Imports System.Text.RegularExpressions

'.........
'.........

dim reg as new Regex("\w*[ - ]")
dim myString as string = reg.Match(myString).Value

'.........
'.........

Hope it helps! [hourglass]

---
If gray hair is a sign of wisdom, then talk like a pirate ninja monkey if you get the time to get funky once a week.
Wo bu zhi dao !
 
Now, if all the OP needs is the first 5 characters of his input string, isn't
Code:
mystring = "M2549 - 2598".Substring(0, 5)
the most efficient way to go?

Bob
 
Thanks to all of you who spare some time and looked at my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top