There is an array of string elements for example:

 mas[0] = "1_30092016_12"; mas[1] = "2_30092016_13"; mas[2] = "3_30022016_14"; mas[3] = "4_30012016_15"; 

How to select items from it by date (the second part of the name after _)?

  • five
    Doesn't it bother you that one of your dates is February 30th? )) - Astronavigator
  • 2
    @Astronavigator, this is like in the work of "12 months", until we complete this task, it will be February 30, February 31, 32 fe ..... - Dmitry Chistik

4 answers 4

 string[] mas = new string[] { "1_30092016_12", "2_30092016_13", "3_30022016_14", "4_30012016_15" }; foreach (var i in mas) { string[] myArray = i.Split('_'); Console.WriteLine(myArray[1]); // второй элемент массива содержит строки с датами } 

    Here's an example for you and String.Format and Linq and work with DateTime , learn about health

     mas.Where(x=>x.Contains(string.Format("_{0}_", (new DateTime(2016, 9, 30)).ToString("ddMMyyyy")))); 
       private void SearchDate() { string[] mas = new string[] { "1_30092016_12", "2_30092016_13", "3_30022016_14", "4_30012016_15" }; string search = "30012016"; for (int i = 0; i < mas.Length; i++ ) { if (mas[i].Contains(search)) { MessageBox.Show(mas[i]); } } } 

        You can use LINQ. In the simplest case, selecting an item by date will look like this:

         var m = from s in mas where s.Contains("30022016") select s; textBox1.Text = m.FirstOrDefault(); 
        • and if I have 2 entries with the same number but different indexes, how to display all? - Side Kick
        • @SideKick, use string.Join - Grundy
        • Is it possible with an example? some days in C #)) - Side Kick