-4

I have a string that I am grabbing that is input as:

05/22/2015
Eligibility
05/06/2015
Date of Death

I need 05/06/2015. The dates will change as the program runs through a database, and I am just a little unsure on how to always be grabbing the correct one.

Rinktacular
  • 1,116
  • 3
  • 19
  • 45
  • 1
    The requirement here is extremely unclear. – garryp May 22 '15 at 18:37
  • Can you include what you've tried and narrow down the exact output you're expecting? Also, have you researched this? I used your exact original title in google and found tons of plausible solutions :) – Carrie Kendall May 22 '15 at 18:37
  • So it's always the third line? Just call 'Split' as detailed here: http://stackoverflow.com/questions/1547476/easiest-way-to-split-a-string-on-newlines-in-net And then grab the appropriate element from the array. – Cortright May 22 '15 at 18:39
  • 1
    I am sorry for the unclear question. I am very new to the site in general and realize I was too vague with it. It is always the third line. Thank you for the recommendation. – Rinktacular May 22 '15 at 18:44
  • Welcome to the site! As a tip for next time, I'd suggest you browse through the [help center](http://stackoverflow.com/help) as it has some really great tips on how to get started and what kind of questions are on-topic here. – Carrie Kendall May 22 '15 at 18:45
  • 1
    @CarrieKendall Understood. Thank you for being so welcoming! I will make sure I look at the help center before posting again. – Rinktacular May 22 '15 at 18:49

2 Answers2

-1

If I understand you correctly, your string has 4 lines and you want the part of the string between linebreak 2 and 3.

Search for the positions of the second and third linebreak \n and use the substring derived of these positions.

MilConDoin
  • 734
  • 6
  • 24
  • yes that is correct. I am only interested in grabbing the second date (05/06/2015) without either title or the first date. – Rinktacular May 22 '15 at 18:51
-1

So you need the second date only? Is it always going to be the 3rd line? If so you can do

var secondDate = myData.Split(new [] { Environment.NewLine }, StringSplitOptions.None)[2];

If the new line isn't for certain; /n vs /r/n, use:

var secondDate = myDate.Split(Environment.Newline.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[2];
Jacob Roberts
  • 1,725
  • 3
  • 16
  • 24
  • This depends on the environment, but will most likely return `Eligibility`. – Rawling May 22 '15 at 18:49
  • 1
    @Rawling An array uses index, indexes start at 0. If I use `[1]` then the outcome would have been `Eligibility`. – Jacob Roberts May 22 '15 at 18:52
  • 1
    [`String.Split(char[])`](https://msdn.microsoft.com/en-us/library/b873y76a.aspx) splits on any of the characters in the input array. Assuming the input has standard C# linebreaks, the resulting array will be `{ "05/22/2015", "", "Eligibility", "", "05/06/2015", "", "Date of Death" }`, the `[2]`th element of which is `"Eligibility"`. – Rawling May 22 '15 at 19:55
  • @Rawling thanks for the breakdown. I've adjusted my answer to use a string array. – Jacob Roberts May 22 '15 at 20:16