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.