2012-10-19 99 views
0

有没有办法指定我想从数组字符串中提取哪些字符? 这不是,代码如下,论坛只是不喜欢它作为文本。VB2010从数组字符串中提取特定字符

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”

我工作的真正的应用程序,文件路径具有的第一个目录总是与项目数开始,所以我想拉工号,并把它变成一个文本框,在VB 2010年 示例:Q:\ 2456_customer_name .... \ file.xls 我希望能够再次指向数字,但是如果我知道主目录将始终以作业号开始,然后我应该能够指向字符[4-7]并将其放入字符串中。我想我知道这个概念,但是不太了解VB,可以把它放在一起。

任何帮助将不胜感激。

回答

0

正则表达式将工作

Dim numRegex As New Regex("\d+") 
Dim number As String = numRegex.Match("abc1234blahblah").Value 
+0

谢谢,这工作完美!非常感激。 – cheapkid1

1

可以使用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”。

+0

谢谢,这工作。 – cheapkid1