Split a string in vb.net

Chris Jones
Chris Jones used Ask the Experts™
on
Hello,

i am trying to split a string in vb.net

here is an exmpale of the string

Degrees_Accounting.html

all i want from the string is  "Accounting"

here is the code but it only splits on the first name EXAMPLE Degrees

        Dim values As String() = Files.Split(ControlChars.Lf, "."c).Where(Function(s) s.Contains("_")).[Select](Function(s) s.Split("_"c)(0)).ToArray()

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Im not sure if this is what you are looking for. It strips off the file ext and then splits the name and returns the second part of the file name.  Your left with Accounting.

 Dim FileName As String = "Degrees_Accounting.html"
        Dim Newfilename As String() = FileName.Split(".")
        Dim FixedFileName As String = Newfilename(0).Split("_")(1)
        MsgBox(FixedFileName)

Open in new window

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Another approach:

Dim filename As String = System.IO.Path.GetFilenameWithoutExtension("Degrees_Accounting.html")
Dim extracted As String = filename.Substring(filename.LastIndexOf("_") + 1)

Open in new window

if you still want to go for linq, here is the correct linq for this:-

Dim values As String() = Files.Split(ControlChars.Lf, "."c).Where(Function(s) s.Contains("_")).[Select](Function(s) s.Split("_"c)(1)).ToArray()

your index was not correct...
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Hi,

u can do this like,

Dim values As String() = Files.Split(ControlChars.Lf, "."c).Where(Function(s) s.Contains("_")).[Select](Function(s) s.Split("_"c)(1)).ToArray()
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I don't think LINQ is a good choice for this purpose. It's not immediately clear what that query is doing or returning. I believe it hinders readability in this case.

@ExpertLogin_890
It is poor etiquette to copy another expert's post exactly and claim it as your own. If you see a flaw in another post, no one will fault you in posting a correction so long as you credit the original author. Flat out plagiarism is not well received at EE  = )
Top Expert 2011
Commented:
Please try:

Dim fn As String = "Degrees_Accounting.html"
Dim names As String() = fn.Split("."c, "_"c)

Console.WriteLine(names(1))

' Output
'
' Accounting

Open in new window

Dim strName As String = "Degrees_Accounting.html"
        Console.WriteLine(strName.Split(".")(0).Split("_")(1))
        Console.ReadKey()
Chris JonesLead Application Web Developer

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial