Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用变量查找字符串[]中的子字符串匹配项_C#_Arrays_Regex_String_For Loop - Fatal编程技术网

C# 使用变量查找字符串[]中的子字符串匹配项

C# 使用变量查找字符串[]中的子字符串匹配项,c#,arrays,regex,string,for-loop,C#,Arrays,Regex,String,For Loop,我试图使用for循环遍历pdf excel文件,找到包含员工ID的所有行,并将匹配的完整行添加到函数中包含的列表中。 如何遍历字符串[]并找到前8个字符的匹配位置 员工信息示例 98113457 Abaile, Volker DB Vacation Available Days 48976143 Asif, Gamal DB Vacation Available 65282785 Affe, Sandra DB Vacation Available Days 98113457 Abaile,

我试图使用for循环遍历pdf excel文件,找到包含员工ID的所有行,并将匹配的完整行添加到函数中包含的列表中。 如何遍历字符串[]并找到前8个字符的匹配位置

员工信息示例

98113457 Abaile, Volker DB Vacation Available Days
48976143 Asif,  Gamal  DB Vacation Available
65282785 Affe, Sandra DB Vacation Available Days
98113457 Abaile, Volker DB Time Account Hours
65282785 Affe, Sandra DB Vacation Carryover Days
48976143 Asif, Gamal  DB Time Account Hours
代码:

string[]words3=words2.ToArray();
对于(int j=7;j
如果要解析上述行,并且具有相同类型的行,请尝试

var words = new List<string>
{
     "98113457 Abaile, Volker DB Vacation Available Days",
     "48976143 Asif,  Gamal  DB Vacation Available",
     "65282785 Affe, Sandra DB Vacation Available Days",
     "98113457 Abaile, Volker DB Time Account Hours",
     "65282785 Affe, Sandra DB Vacation Carryover Days",
     "48976143 Asif, Gamal  DB Time Account Hours"
};
foreach (var word in words)
{
     var id = word.Substring(0, 8);
     var name = word.Split(',')[0].Split(' ')[1];
     var type = word.Split(',')[1].Trim().Split(' ')[0];
     var db = word.Split(',')[1].Trim().Split(' ')[1];
     var index = word.IndexOf(db) + 2;
     var others = word.Substring(index, word.Length - index);
}
var words=新列表
{
“98113457 Abaile,Volker DB假期可用天数”,
“48976143 Asif,Gamal DB假期可用”,
“65282785阿菲,桑德拉DB假期可用天数”,
“98113457 Abaile,Volker DB时间账户小时数”,
“65282785阿菲,桑德拉DB假期结转日”,
48976143 Asif,Gamal DB时间账户小时数
};
foreach(单词中的var单词)
{
var id=字子串(0,8);
var name=word.Split(',')[0]。Split('')[1];
var type=word.Split(',')[1].Trim().Split('[0];
var db=word.Split(',')[1].Trim().Split(''[1];
var指数=单词索引(db)+2;
var others=单词.子字符串(索引,单词.长度-索引);
}

感谢评论中的所有建议,这对我正在尝试的工作起到了作用

            int matches = 0;
            string lastID = words3[7].Substring(0, 8);
            for (int j = 7;  j < words2.Count(); j++)
            { 
                 string  results  = null;
                //"results" is the current employee information line  
                results = words3[j];

                //The ID number of that employee 
                 string id = results.Substring(0, 8);

                //Find and get all of the lines that start with employee ID from string[] words3
                if ( !words3[j].Contains(lastID))
                 { 

                    //j-matches are the indexes that match
                     for (int x = j - matches; x < j; x++)
                     {
                         Console.WriteLine(words3[j]);
                     }
                    //Found all of the matches so reset for new upcoming IDs
                     matches = 0;
                 }
                else
                {
                    matches++;
                }  

                if (lastID != id)
                 {
                     lastID = id;
                 }
            }
int匹配=0;
字符串lastID=words3[7]。子字符串(0,8);
对于(int j=7;j
这不应该是
results=word3[j]?此代码和您的描述似乎不匹配,并且不清楚您希望此代码做什么。什么是
results=@“words3[j]”应该怎么做?如果您正在从excel中读取,为什么不创建一个Employee类并用excel中的信息实例化它呢。然后,它将提供更多选项来说明如何使用该列表。@user1666620我正在阅读一个类似excel的PDF文件format@Cscience18:该变量包含当前行中的员工ID。当你说查找所有“前8个字符匹配”时,匹配什么?匹配特定id,如
98113457
?或者任何一个列表?或者任何看起来像8位数的都是有效的?
            int matches = 0;
            string lastID = words3[7].Substring(0, 8);
            for (int j = 7;  j < words2.Count(); j++)
            { 
                 string  results  = null;
                //"results" is the current employee information line  
                results = words3[j];

                //The ID number of that employee 
                 string id = results.Substring(0, 8);

                //Find and get all of the lines that start with employee ID from string[] words3
                if ( !words3[j].Contains(lastID))
                 { 

                    //j-matches are the indexes that match
                     for (int x = j - matches; x < j; x++)
                     {
                         Console.WriteLine(words3[j]);
                     }
                    //Found all of the matches so reset for new upcoming IDs
                     matches = 0;
                 }
                else
                {
                    matches++;
                }  

                if (lastID != id)
                 {
                     lastID = id;
                 }
            }