我如何从变量中拆分

时间:2018-12-12 12:02:50

标签: vb.net visual-studio

我希望能够用“ \”分隔变量的值并将其保存在变量中。我在变量“ C:\ Users \ admin \ test \ test.txt”中,我只想获取文件名,因此可以按名称搜索

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

我这样做是为了测试。

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)

在cmd上输出错误-> System.String []

1 个答案:

答案 0 :(得分:2)

使用

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

将获取不带扩展名的文件名并将其打印到控制台

如果您仍然想使用Split功能,请使用

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