开发者

VB.NET one dimensional string array manipulation difficulty

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.t开发者_StackOverflowxt

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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜