最好的方法来找到其中的细胞的string array它由文本
题
我有一个方块中的文字,即时通讯服从Gedcom(在这里, 和 在这里,)的文件
该文本是平和基本上破碎为"节点"
我是分裂的各个节点上 char并因此将其细分为每个部分(数量的"线"可以有所不同)
我知道0地址总是会ID但在那之后一切都可以在任何地方所以我想来检验每个单元的阵列,看它是否含有正确的标签对我来过程
一个例子是两个节点将会看起来像
0 @ind23815@ INDI <<<<<<<<<<<<<<<<<<< Start of node 1 1 NAME Lawrence /Hucstepe/ 2 DISPLAY Lawrence Hucstepe 2 GIVN Lawrence 2 SURN Hucstepe 1 POSITION -850,-210 2 BOUNDARY_RECT (-887,-177),(-813,-257) 1 SEX M 1 BIRT 2 DATE 1521 1 DEAT Y 2 DATE 1559 1 NOTE * Born: Abt 1521, Kent, England 2 CONT * Marriage: Jane Pope 17 Aug 1546, Kent, England 2 CONT * Died: Bef 1559, Kent, England 2 CONT 1 FAMS @fam08318@ 0 @ind23816@ INDI <<<<<<<<<<<<<<<<<<<<<<< Start of Node 2 1 NAME Jane /Pope/ 2 DISPLAY Jane Pope 2 GIVN Jane 2 SURN Pope 1 POSITION -750,-210 2 BOUNDARY_RECT (-787,-177),(-713,-257) 1 SEX F 1 BIRT 2 DATE 1525 1 DEAT Y 2 DATE 1609 1 NOTE * Born: Abt 1525, Tenterden, Kent, England 2 CONT * Marriage: Lawrence Hucstepe 17 Aug 1546, Kent, England 2 CONT * Died: 23 Oct 1609 2 CONT 1 FAMS @fam08318@ 0 @ind23817@ INDI <<<<<<<<<<< start of Node 3
所以当我完成我有一系列看起来像
address , string 0 = "1 NAME Lawrence /Hucstepe/" 1 = "2 DISPLAY Lawrence Hucstepe" 2 = "2 GIVN Lawrence" 3 = "2 SURN Hucstepe" 4 = "1 POSITION -850,-210" 5 = "2 BOUNDARY_RECT (-887,-177),(-813,-257)" 6 = "1 SEX M" 7 = "1 BIRT " 8 = "1 FAMS @fam08318@"
所以我的问题是什么是最好的方式搜索的上述阵列,看看这小区具有性别标记或标签名或FAMS标签
这是我的代码
private int FindIndexinArray(string[] Arr, string search) { int Val = -1; for (int i = 0; i < Arr.Length; i++) { if (Arr[i].Contains(search)) { Val = i; } } return Val; }
但它似乎效率低下,因为最终我叫了两次,以确保它不会返回一个-1
就像这样
if (FindIndexinArray(SubNode, "1 BIRT ") != -1) { // add birthday to Struct I.BirthDay = SubNode[FindIndexinArray(SubNode, "1 BIRT ") + 1].Replace("2 DATE ", "").Trim(); }
对不起这是一个较长的职位,但希望你们将有一些专家的意见
解决方案
可以使用数组类的静态方法的FindAll: 它将返回字符串本身不过,如果该工程..
string[] test = { "Sex", "Love", "Rock and Roll", "Drugs", "Computer"};
Array.FindAll(test, item => item.Contains("Sex") || item.Contains("Drugs") || item.Contains("Computer"));
在=>表示的λ表达式。基本上,没有一个具体的实现方法。 你也可以做到这一点,如果LAMDA让你浑身起鸡皮疙瘩。
//Declare a method
private bool HasTag(string s)
{
return s.Contains("Sex") || s.Contains("Drugs") || s.Contains("Computer");
}
string[] test = { "Sex", "Love", "Rock and Roll", "Drugs", "Computer"};
Array.FindAll(test, HasTag);
其他提示
约简单正则表达式?
^(\d)\s=\s\"\d\s(SEX|BIRT|FAMS){1}.*$
首先组捕获地址,第二组标记。
此外,它可能是更快地转储所有数组项转换为字符串,并立即做你在一大堆正则表达式。
“但似乎效率不高,因为我最终调用它两次,以确保它不返回-1”
返回的值复制到变量测试以防止多次调用之前。
IndexResults = FindIndexinArray(SubNode, "1 BIRT ")
if (IndexResults != -1)
{
// add birthday to Struct
I.BirthDay = SubNode[IndexResults].Replace("2 DATE ", "").Trim();
}
在for循环的方法FindIndexinArray SHD打破,一旦你找到一个匹配,如果你感兴趣的只是第一场比赛。
不隶属于 StackOverflow