在 .NET 中,查找从 OBJ 文件读取的数组部分的开始和结束索引

In .NET, find the start and end indices of a section of an array read in from an OBJ file

我有一个 OBJ 文件,我将其读入对象数组。然后我使用 Array.IndexOf 首先找到我要处理的对象层部分,然后获取该部分中以“vt”开头的行的开始和结束索引。我可以获得对象层的正确索引,但其他两个索引显示为 -1.

这是带有一个对象的 OBJ 的截断版本。

o Layer_1_Hull
.
.
.
o Layer_2_Mainsail                         '=== Finds index of specified object layer first,
v 13.747500 283.669006 411.285187          '=== Layer_2_Mainsail object layer in this case
v 13.747500 283.669006 411.275177
v 13.797300 283.483002 411.275177
vt 0.900824 0.386317                       '=== Finds index of the first instance of "vt"
vt 0.900895 0.386336
vt 0.871146 0.496693
vt 0.871075 0.496673                       '=== Finds index of the last instance of "vt"
vn -0.9660 -0.2587 0.0000
vn -0.9660 0.2587 0.0000
vn -0.7071 0.7072 0.0000
vn -0.2592 0.9658 0.0000

我使用的代码

    Option Explicit On
    .
    .
    .
    Dim objArray As String() = File.ReadAllLines("C:\Users\Dude\Project\Sailboat.obj")
    Dim layerIndex As Integer = Array.IndexOf(objArray, "o " + "Layer_2_Mainsail")
    Dim uvStartIndex As Integer = Array.IndexOf(objArray, "vt ", layerIndex)       '=== Returns -1, why??
    Dim uvEndIndex As Integer = Array.IndexOf(objArray, "vn ", uvStartIndex) - 1   '=== index out of bounds

感谢您在故障排除方面的帮助。

我会这样写:

 Dim lines = File.ReadLines("C:\Users\Dude\Project\Sailboat.obj").
      SkipWhile(Function(line) Not line.StartsWith("o " + "Layer_2_Mainsail")).
      SkipWhile(Function(line) Not line.StartsWith("vt ")).
      TakeWhile(Function(line) Not line.StartsWith("vn "))

在此处查看它的工作原理(link 还将显示您可能需要添加的任何 Imports 指令):

https://dotnetfiddle.net/767zkX

至于为什么现有代码失败,我怀疑目标文本后面有额外的空格,所以该行与搜索键不完全匹配。