我把这部分代码放到了一个可视化的基础项目中......
Dim fileReader1 = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
Dim Part1A = "<b id=""Blahblah"">"
Dim Part1B = "</b>"
Dim fileSplit1 = fileReader1.Split(New String() {Part1A}, StringSplitOptions.None)
fileReader1 = fileSplit1.Last
fileSplit1 = fileReader1.Split(New String() {Part1B}, StringSplitOptions.None)
fileReader1 = fileSplit1.First
...我试图为它做一个循环来增加所有变量的数字1
。像这样......
For i As Integer = 1 To 10
Dim ("fileReader" & i) = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
...
Next
...但我收到错误!知道怎么应该是语法?
答案 0 :(得分:3)
不幸的是,变量不喜欢这样命名。但是,您可以使用数组:
Dim fileReader(10) as String
For i As Integer = 1 To 10
fileReader(i) = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
Dim Part1A = "<b id=""Blahblah"">"
Dim Part1B = "</b>"
Dim fileSplit1 = fileReader(i).Split(New String() {Part1A}, StringSplitOptions.None)
fileReader(i) = fileSplit1.Last
fileSplit1 = fileReader(i).Split(New String() {Part1B}, StringSplitOptions.None)
fileReader(i) = fileSplit1.First
Next
您的其余代码没问题,您只需要将fileReader1的每个实例更改为fileReader(i)。
如果Part1A和Part1B必须为每个i设置唯一值,那么您将以相同的方式创建一个数组:
Dim fileReader(10) as String
Dim PartA(10) as String
Dim PartB(10) as String
For i As Integer = 1 To 10
fileReader(i) = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
Dim PartA(i) = "<b id=""Blahblah"">"
Dim PartB(i) = "</b>"
Dim fileSplit1 = fileReader(i).Split(New String() {PartA(i)}, StringSplitOptions.None)
fileReader(i) = fileSplit1.Last
fileSplit1 = fileReader(i).Split(New String() {PartB(i)}, StringSplitOptions.None)
fileReader(i) = fileSplit1.First
Next
然而,将PartA和PartB放入数组是不必要的,如果它们总是具有相同的值,它们将在您给出的示例中具有相同的值。如果示例保持原样,则只需将fileReader1更改为fileReader(i)。