【问题标题】:Separating Delimited Variable into array elements in VB.NET在 VB.NET 中将分隔变量分隔为数组元素
【发布时间】:2012-03-05 04:18:55
【问题描述】:

vb.net中的long变量包含以下信息,

Dim g As String = "$C:\Program Files\Cavaj Java Decompiler\cavaj.exe$C:\Users\Yoosuf\AppData\Local\Google\Chrome\Application\chrome.exe$C:\Program Files\DVD Maker\dvdmaker.exe$C:\Program Files\Adobe\Adobe Photoshop CS2\ImageReady.exe$C:\Program Files\Java\jre6\bin\javaws.exe$"

$ 符号用作分隔每个项目的分隔符。我需要在每个路径的末尾添加 exe 文件名到列表框。但是,将变量检索到单个数组元素的初始过程无法正常工作。

Dim strArr() As String = g.Split("$")  'This variable is empty
For count = 0 To strArr.Length - 1
Dim arr As String = strArr(count).Split("\")

Dim strval As String = ""
For i As Integer = 3 To arr.Length - 1
        strval = arr(i)
        Dim j As Integer = arr.Length - 1
        strval = arr(j)
        Dim result As String = strval.Substring(g.Length - 5)
        result = g.Substring(g.LastIndexOf("\") + 1)
        ListBox1.Items.Add(result)
 Next
Next

【问题讨论】:

  • 对我来说很好。 strArr.Length = 7。我看不出你的代码有什么问题

标签: vb.net arrays variables delimiter


【解决方案1】:

无需完成所有这些工作。 System.IO.Path 类具有为您执行此操作的方法。您想使用 System.IO.Path.GetFileNameSystem.IO.Path.GetFileNameWithoutExtension。由于您已经拆分了所有文件路径,只需将这些路径传递给上述任一方法并将结果添加到您的列表框。

Dim strArr() As String = g.Split("$")
For Each path As String In strArr
    ListBox1.Items.Add(System.IO.Path.GetFileName(path))
Next

【讨论】:

    【解决方案2】:

    请参考下面的代码和相关的 cmets。此外,我还根据您的需要注释掉了一些我认为不需要的代码。

    Dim strArr() As String = g.Split("$")  'This variable is empty
            For count = 0 To strArr.Length - 1
                Dim arr() As String = strArr(count).Split("\") ' Split returns an array
    
                Dim strval As String = ""
                For i As Integer = 3 To arr.Length - 1
                    'strval = arr(i)
                    Dim j As Integer = arr.Length - 1
                    strval = arr(j)
                    'Dim result As String = strval.Substring(g.Length - 5)
                    'result = g.Substring(g.LastIndexOf("\") + 1)
                    ListBox1.Items.Add(strval)
                Next
            Next
    

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 2019-05-21
      • 2021-06-24
      • 2017-10-30
      相关资源
      最近更新 更多