1

I have an array of string, I want to take all the string in an interval of this array until string does not contains something.

Something like:

string [] arrayReading = {
  "e","x","a","takefromhere",
  "keeptaking","keeptaking","dont'ttakefromhere","m","p","l","e"
};

I have tried:

        List<string> result = null;

        for (int i = 0; i < arrayReading.Length; i++)
        {
            if (arrayReading[i].Contains("takefromhere"))
            {
                result.Add(arrayReading[i]);

                if (!arrayReading[i + 1].Contains("dont'ttakefromhere"))
                {
                    result.Add(arrayReading[i + 1]);

                    if (!arrayReading[i + 2].Contains("dont'ttakefromhere"))
                    {
                        rescription.Add(arrayReading[i + 1]);
                    }
                }
            }
        }

Seems working but it's not really dynamic as I want it, because maybe I need to take 20 values between "takefromhere" and "don'ttakefromhere".

3
  • So you will always have a start value right, what about and end value, will there always be one or not? If there's no end value, just take them all or otherwise in between them correct? Commented Feb 27, 2020 at 14:05
  • 2
    List<string> result = arrayReading.SkipWhile(item => item != startItem).TakeWhile(item => item != stopItem).ToList(); and you'll have {"takefromhere", "keeptaking", "keeptaking"} Commented Feb 27, 2020 at 14:05
  • 2
    LINQ's SkipWhile and TakeWhile allow you to almost translate your specification literally. Commented Feb 27, 2020 at 14:05

1 Answer 1

2

When querying you can try Linq:

 using System.Linq;

 ...

 List<string> result = arrayReading
   .SkipWhile(item => item != "takefromhere") 
   .TakeWhile(item => item != "dont'ttakefromhere")
   .ToList();

Or if you want good old loop solution:

 List<string> result = new List<string>(); 

 bool taking = false;

 foreach (string item in arrayReading) {
   if (!taking) 
     taking = item == "takefromhere";

   if (taking) {
     if (item == "dont'ttakefromhere")
       break;

     result.Add(item);
   } 
}

Let's have a look:

Console.Write(string.Join("; ", result));

Outcome:

takefromhere; keeptaking; keeptaking
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, didn't know this property SkipWhile/TakeWhile!
@RobNone they are not properties... Also this answer could address what the OP is doing wrong in their code above so user understands what is wrong and anyone else that may come across the same issue, but this deviates from that by providing a different solution.
@Çöđěxěŕ: or we could have a new answer that addresses what the OP is doing wrong, and you could even be the one to write that. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.