I need to search through a string array to find every occurrence of Parameters Table, and between each Parameters Table and the next, get another string from a specified index (that remains constant). I have been doing this like so:public List<string> findlistOfNames(string[] arrayToLookForNames){    List<string> listOfNames = new List<string>();    const string separator = Parameters Table; //This is the string I am searching for    var cuttedWords = arrayToLookForNames.SkipWhile(x => x != separator).Skip(1);    while (cuttedWords.Any())    {        var variable = cuttedWords.TakeWhile(x => x != separator).ToArray();        cuttedWords = cuttedWords.Skip(variable.Length + 1);        listOfNames.Add(variable[2]); //This (always index 2) needs to be added to the list    }    return listOfNames;}This works but too slowly. Is there a better way to do this?Here is a snippet of string[] arrayToLookForNames:Parameters Table041Baro PressurehPa AFD2recordedParameters Table042Baro Settingin-hg
Faster method to find data using search word?
c#;performance;strings;search
You are chaining methods deeper and deeper, so the longer the loop runs, the slower it gets.You start out with cuttedWords as the expression arrayToLookForNames.SkipWhile(x => x != separator).Skip(1). Every time that you use cuttedWords it will evaluate that expression again, and every method that you chain into it will also be evaluated from there on.After one iteration in the loop cuttedWords is (assuming that the record length is 7):arrayToLookForNames.SkipWhile(x => x != separator).Skip(1).Skip(7)After ten iterations it's:arrayToLookForNames.SkipWhile(x => x != separator).Skip(1).Skip(7).Skip(7).Skip(7).Skip(7).Skip(7).Skip(7).Skip(7).Skip(7).Skip(7)If you just keep track of how many items to skip instead of chaining expressions, it will get faster:public List<string> findlistOfNames(string[] arrayToLookForNames){    List<string> listOfNames = new List<string>();    const string separator = Parameters Table; //This is the string I am searching for    int skip = 0;    while (arrayToLookForNames[skip] != separator) {      skip++;    }    skip++;    while (skip < arrayToLookForNames.Length)    {        string[] variable = arrayToLookForNames.Skip(skip).TakeWhile(x => x != separator).ToArray();        skip += variable.Length + 1;        listOfNames.Add(variable[2]); //This (always index 2) needs to be added to the list    }    return listOfNames;}