文字列配列のどのセルを検索するための最良の方法は、テキストが含まれています

StackOverflow https://stackoverflow.com/questions/855901

質問

私は(ここここの)ファイル

テキストは平坦であり、基本的に「ノード」に分割

私は(「行」の量を変えることができる)の\ Rチャー上の各ノードを分割し、従って、その部品の各々にそれを細分化しています

私は0アドレスは常にIDになります知っているが、私は私がproccessすることが正しいのタグが含まれているかどうかを確認するために、配列の各セルをテストしたいので、すべてがどこでもすることができた後、

2つのノードが

どのように見えるかの

タグ

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

So a when im done i have an array that looks like

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@"

So my question is what is the best way to search the above array to see which Cell has the SEX tag or the NAME Tag or the FAMS Tag

this is the code i have

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;
}

But it seems inefficient because i end up calling it twice to make sure it doesnt return a -1

Like so

if (FindIndexinArray(SubNode, "1 BIRT ") != -1) { // add birthday to Struct I.BirthDay = SubNode[FindIndexinArray(SubNode, "1 BIRT ") + 1].Replace("2 DATE ", "").Trim(); }

申し訳ありませんが、これは長い記事ですが、うまくいけば、あなたたちは、いくつかの専門家の助言を持つことになります。

役に立ちましたか?

解決

Arrayクラスの静的メソッドのfindAllを使用することができます。 それが動作するかどうか、それは、しかし、文字列自体を返します..

string[] test = { "Sex", "Love", "Rock and Roll", "Drugs", "Computer"};
Array.FindAll(test, item => item.Contains("Sex") || item.Contains("Drugs") || item.Contains("Computer"));

=>は、ラムダ式を示しています。基本的には具体的な実装のない方法。 ラムダはあなたにゾッとを与える場合にも、この操作を行うことができます。

//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}.*$

最初のグループは、アドレス、第二群のタグを捕捉する。

また、文字列にすべての配列項目をダンプし、一度に全体の多くであなたの正規表現を行うために迅速かもしれません。

  

この「私は二度、それが返すdoes notの-1を確認するために、それを呼び出してしまうので、しかし、それは非効率です」

あなたが複数の呼び出しを防ぐためにテストする前に、

変数に返された値をコピーします。

IndexResults = FindIndexinArray(SubNode, "1 BIRT ")
if (IndexResults != -1)
        {
            // add birthday to Struct 
            I.BirthDay = SubNode[IndexResults].Replace("2 DATE ", "").Trim();
        }
あなたが唯一の最初の試合に興味がある場合は、一致するものを見つけたら、

ループするための方法でFindIndexinArrayが壊れSHDます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top