0

I am having some problems with manipulating a one dimensional string array in VB.NET and would like your assistance please.

My objective is to get 4 variables (if possible) from a file path. These variables are: myCountry, myCity, myStreet, Filename. All declared as string. The file location is also declared as string. so I have:

Dim filePath As String

to illustrate my problem, and what I am trying to do, I have the following examples:

1-

C:\my\location\is\UK\Birmingham\Summer Road\this house.txt.

In this example myCountry would be= UK. myCity= Birmingham. myStreet=Summer Road. Filename=this house.txt

2-

C:\my Location\is\France\Lyon\that house.txt.

here myCountry=France. myCity=Lyon. There is no street. Filename=that house.txt

3-

C:\my Location is\Germany\the other house.txt

Here myCountry=Germany. No city. No street. Filename=the other house.txt

What I am trying to say is I have no idea beforehand about the length of the string or the position of the variables I want. I also don't know if I am going to find/get a city or street name in the path. However I do know that I will get myCountry and it will be one of 5 options: UK, France, Germany, Spain, Italy.

To tackle my problem, the first thing I did was:

Dim pathArr() As String = filePath.Split("\")

To get the FileName I did:

FileName = pathArr.Last

To get myCountry I did:

    If filePath.Contains("UK") Then
        myCountry = "UK"
    ElseIf filePath.Contains("France") Then
        myCountry = "France"
    ElseIf filePath.Contains("Germany") Then
        myCountry = "Germany"
    ElseIf filePath.Contains("Spain") Then
        myCountry = "Spain"
    ElseIf filePath.Contains("Italy") Then
        myCountry = "Italy"
    End If

In trying to figure out myCity and myStreet (and whether they exist in the string in the first place) I started with:

Dim ind As Integer = Array.IndexOf(pathArr, myCountry)

To get the index of the myCountry string. I thought I could make my way from there but I am stuck and don't know what to do next. Any help will be appreciated.

1
  • Use regular expressions to parse the path. I'm not an expert in those so you might want to ask how to use Regex to parse a path string like that. Commented May 14, 2010 at 22:48

1 Answer 1

2

Using pathArr was a good idea already. You can use an index variable to loop through the components. The following code is untested but should get you started:

Dim countries As New String() {"UK", "France", "Germany", "Spain", "Italy"}

Dim pathArr() As String = filePath.Split("\")               ' " <-- this is just to fix SO syntax highlighting

' Find the component containing the country '
Dim i = 0
Do Until i >= pathArr.Length OrElse countries.Contains(pathArr(i))
    i += 1
Loop

If i >= pathArr.Length - 1 Then
    ... ' Error: No Country found or Country is last component'
Else
    myCountry = pathArr(i)
    i += 1
    If i < pathArr.Length - 1 Then
        myCity = pathArr(i)
        i += 1
        If i < pathArr.Length - 1 Then
            myStreet = pathArr(i)
            i += 1
            If i < pathArr.Length - 1 Then
                ... ' Error: Too many components '
            End If
        End If
    End If
    FileName = pathArr(i)
End If
Sign up to request clarification or add additional context in comments.

1 Comment

many thanks for your help. I tweaked it a bit and it worked very well. Again, many thanks

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.