Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET一维字符串数组操作难度_Vb.net_String_Arrays - Fatal编程技术网

VB.NET一维字符串数组操作难度

VB.NET一维字符串数组操作难度,vb.net,string,arrays,Vb.net,String,Arrays,我在VB.NET中操作一维字符串数组时遇到一些问题,希望您提供帮助 我的目标是从文件路径中获取4个变量(如果可能)。这些变量是: myCountry、myCity、myStreet、文件名。全部声明为字符串。文件位置也声明为字符串。因此,我: Dim filePath As String 为了说明我的问题以及我正在尝试做的事情,我举了以下示例: 一,- C:\my\location\is\UK\Birmingham\Summer Road\this house.txt 在这个例子中,myCou

我在VB.NET中操作一维字符串数组时遇到一些问题,希望您提供帮助

我的目标是从文件路径中获取4个变量(如果可能)。这些变量是: myCountry、myCity、myStreet、文件名。全部声明为字符串。文件位置也声明为字符串。因此,我:

Dim filePath As String
为了说明我的问题以及我正在尝试做的事情,我举了以下示例:

一,-

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

在这个例子中,myCountry将是=UK。我的城市=伯明翰。myStreet=夏季道路。Filename=this house.txt

二,-

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

这里我的国家=法国。我的城市=里昂。没有街道。Filename=that house.txt

三,-

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

这里我的国家=德国。没有城市。没有街道。Filename=other house.txt

我想说的是,我事先不知道字符串的长度或者我想要的变量的位置。我也不知道我是否会在路径中找到/获得城市或街道名称。 不过我知道我会得到我的国家,这将是5个选择之一:英国,法国,德国,西班牙,意大利

为了解决我的问题,我做的第一件事是:

Dim pathArr() As String = filePath.Split("\")
要获取文件名,我执行了以下操作:

FileName = pathArr.Last
为了得到我的国家,我做了:

    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
在尝试找出myCity和myStreet(以及它们是否首先存在于字符串中)时,我从以下内容开始:

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

获取myCountry字符串的索引。我以为我可以从那里走出来,但我被卡住了,不知道下一步该怎么办。任何帮助都将不胜感激。

使用
pathArr
已经是个好主意了。可以使用索引变量在组件之间循环。以下代码未经测试,但应该可以让您开始:

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
Dim国家作为新字符串(){“英国”、“法国”、“德国”、“西班牙”、“意大利”}
Dim pathArr()作为字符串=filePath.Split(“\””)=pathArr.Length或lse countries.Contains(pathArr(i))
i+=1
环
如果i>=路径长度-1,则
…'错误:未找到国家/地区或国家/地区是最后一个组件'
其他的
myCountry=pathArr(一)
i+=1
如果我<路径长度-1,那么
myCity=pathArr(一)
i+=1
如果我<路径长度-1,那么
myStreet=pathArr(i)
i+=1
如果我<路径长度-1,那么
…'错误:组件太多'
如果结束
如果结束
如果结束
FileName=pathArr(i)
如果结束

使用正则表达式解析路径。我不是这方面的专家,因此您可能想问如何使用正则表达式解析这样的路径字符串。非常感谢您的帮助。我对它进行了一些调整,效果非常好。再次,非常感谢