Avatar of Chris Jones
Chris JonesFlag for United States of America

asked on 

Split a string in vb.net

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

Visual Basic.NET.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
Chris Jones
Avatar of joef12345
joef12345

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of BuggyCoder
BuggyCoder
Flag of India image

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...
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()
Avatar of kaufmed
kaufmed
Flag of United States of America image

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  = )
SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Chris Jones
Chris Jones
Flag of United States of America image

ASKER

Thanks
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo