0

Is there a way to specify which characters I want to pull out of an array string? This is not, code below, the forum just didn't like it as text.

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"

Then I'd like to put those into a string: "1234"

True application I'm working on, a filepath has the first directory always start with the project number, so I want to pull the job number and put it into a textbox in VB 2010. Example: Q:\2456_customer_name....\file.xls I want to be able to again point to the numbers, but if I know that the main directory will always start with the job number, then I should be able to just point to characters [4-7] and put it in a string. I think I know the concept, but don't know VB well enough to put it together.

Any help would be much appreciated.

2 Answers 2

1

You can use the Substring function:

Dim a = "abc1234blahblah"
Dim b = a.Substring(3, 4) ' b now contains "1234"

Thinking about it more, could it be possible for the drive the file is on to be a UNC path like \\MyServer\SomeShare\9876_CustomerName\file.xls? If so, extracting the number would be a bit more tricky. I tried to account for all possible ways of specifying the file:

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

Which outputs "5678" for all of the examples.

Sign up to request clarification or add additional context in comments.

Comments

0

A regular expression would work

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.