有没有一种方法可以指定我想从数组字符串中提取哪些字符?这不是,下面的代码,论坛只是不喜欢它作为文本。
For example: abc1234blahblah and I want to point from the left, characters [4-7]
Character 4 = "1"
Character 5 = "2"
Character 6 = "3"
Character 7 = "4"
然后我想把它们放到一个字符串中:"1234“
我正在开发一个真正的应用程序,文件路径的第一个目录总是以项目编号开头,所以我想提取作业编号并将其放入VB2010的文本框中。示例: Q:\2456_customer_name....\file.xls我希望能够再次指向数字,但是如果我知道主目录总是以作业编号开始,那么我应该能够只指向字符4-7并将其放入字符串中。我想我知道这个概念,但对VB还不够了解,无法把它组合在一起。
任何帮助都将不胜感激。
发布于 2012-10-20 01:39:24
正则表达式可以工作
Dim numRegex As New Regex("\d+")
Dim number As String = numRegex.Match("abc1234blahblah").Value
发布于 2012-10-20 01:27:32
您可以使用Substring function
Dim a = "abc1234blahblah"
Dim b = a.Substring(3, 4) ' b now contains "1234"
再想一想,文件所在的驱动器有没有可能是像\\MyServer\SomeShare\9876_CustomerName\file.xls
这样的UNC路径?如果是这样的话,提取数字将会更加棘手。我试图说明指定文件的所有可能方法:
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“。
https://stackoverflow.com/questions/12979300
复制相似问题