简体   繁体   English

带String.Split()的空字符

[英]Empty char with String.Split()

I'm working on OBJ files parser, and I need to get information about faces. 我正在研究OBJ文件解析器,并且需要获取有关面孔的信息。 In OBJ face sometimes looks like that: f 1//1 2//1 3//1 or like that: f 1/1/1 2/4/1 3/2/1 . 在OBJ中,脸有时看起来像是: f 1//1 2//1 3//1或那样: f 1/1/1 2/4/1 3/2/1 I'm trying to get these numbers: 我正在尝试获取以下数字:

int[] facePointerValues = Array.ConvertAll(facePointer.Split('/'), int.Parse);

If " f 1/1/1 2/4/1... " it works, but the problem appears when: " f 1//1 2//1... " because of empty char between "/". 如果“ f 1/1/1 2/4/1... ”可以工作,但是在以下情况下出现问题:“ f 1//1 2//1... ”由于“ /”之间的空字符。 How to fix that? 如何解决?

As peeople mentioned in the comments, you can tell Split() to ignore empty entries, but then you'll end up with an array size of 2 instead of 3. 正如评论中的人们所提到的,您可以告诉Split()忽略空条目,但是最终将得到2而不是3的数组大小。

Alternatively, you can selectively use 0 for empty values: 另外,您可以选择使用0表示空值:

int[] facePointerValues = Array.ConvertAll(facePointer.Split('/'),
                              s => s.Length < 1 ? 0 : int.Parse(s));

This way, you'll still always end up with an array size of 3. 这样,您仍将始终获得3的数组大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM