Avatar of Chris Jones
Chris Jones
Flag 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

8/22/2022 - Mon
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

kaufmed

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

BuggyCoder

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...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ExpertLogin_890

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()
kaufmed

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
wdosanjos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Chris Jones

ASKER
Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.