您可以使用Substring 函数:
Dim a = "abc1234blahblah"
Dim b = a.Substring(3, 4) ' b now contains "1234"
再想一想,文件所在的驱动器是否有可能像 UNC 路径一样\\MyServer\SomeShare\9876_CustomerName\file.xls
?如果是这样,提取数字会有点棘手。我试图说明指定文件的所有可能方式:
Module Module1
Function GetCustomerNumber(filename As String) As String
Dim abspath = IO.Path.GetFullPath(filename)
Dim dir = IO.Path.GetDirectoryName(abspath)
Dim fileParentDirectory = dir.Split(IO.Path.DirectorySeparatorChar).Last
Return fileParentDirectory.Substring(0, 4)
End Function
Sub Main()
Dim a = "C:\5678_CustomerName\file.xls"
Dim b = "\\se1234\Share001\5678_CustomerName\file.xls"
Dim c = "\5678_CustomerName\file.xls"
Dim d = ".\5678_CustomerName\file.xls"
Dim e = "5678_CustomerName\file.xls"
Console.WriteLine(GetCustomerNumber(a))
Console.WriteLine(GetCustomerNumber(b))
Console.WriteLine(GetCustomerNumber(c))
Console.WriteLine(GetCustomerNumber(d))
Console.WriteLine(GetCustomerNumber(e))
Console.ReadLine()
End Sub
End Module
它为所有示例输出“5678”。