我正在尝试使用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();
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 firstResults = results.Substring(0, 8);
//Find and get all of the lines that start with employee ID from string[] words3
if (firstResults.IndexOf(words3) != -1)
{
//pass the full lines with all of the employee's information to a function
}
}
答案 0 :(得分:0)
如果你想解析上面的行并且你有相同类型的行,那就试试吧
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);
}
答案 1 :(得分:0)
感谢评论中的所有建议,这对我想要做的事情有用。
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;
}
}