Oriol
Oriol

Reputation: 147

How can i do a split from a variable

I want to be able to separate the value of a variable by "\" and save it in a variable. I have in the variable "C:\Users\admin\test\test.txt" and I want to get only the name of the file so I can search by name

Sub Main()
        Dim numbers = {"prova.txt", "prova2.txt", "prova3.txt"}
        Dim prova As New ArrayList
        For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\inetpub\wwwroot\manager\Audio", Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.txt")
            Dim words As String() = foundFile.Split(New Char() {"\"})
            Console.WriteLine(words)
            prova.Add(foundFile)
        Next


        Console.Read()
    End Sub

I did that for a test.

Dim s As String = "C:\Users\Sam\Documents\Perls\Main"

        ' Split the string on the backslash character.
        Dim parts As String() = s.Split(New Char() {"\"c})
        Console.WriteLine(parts)

ERROR output on cmd -> System.String[]

Upvotes: 0

Views: 60

Answers (1)

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

Use

Dim s As String = "C:\Users\Sam\Documents\Perls\Main"
Dim finalstring As String = System.IO.Path.GetFileNameWithoutExtension(s)
Console.WriteLine(finalstring)

That gets the filename without its extension and prints it to the console

If you still want to use the Split function , use

Dim parts As String() = Split(s,"\")
For x As Integer = 0 To Ubound(parts) - 1
   Console.WriteLine(parts(x) & vbNewLine)
Next

Upvotes: 2

Related Questions