Starting Index of Substring on the 2nd Occurance of "DATE"?

Hello I’m a bit stuck on a substring method that has to extract dates from a text document.

So there are two different types of dates that are on the document such as the expiry date and signed date.

I have four variables and they are as follows…

strTextFile = whole document
intStartIndex = strTextFile.IndexOf(“DATE”) +4
intFinishIndex = strTextFile.IndexOf(“DAYTIME”) -0
intLength = inFinishIndex - intStartIndex

strSignedDate = strTextFile.Substring(intStartIndex, intLength)

For example: DATE 02/05/2019 DAYTIME

The problem I’m having is that the starting index is starting at the top of the document next to the expiry date, which is then displaying the whole document in the string. Where’s I would like it to be starting at the bottom next to the signed date.

Is there a way to make it so that the substring method will start at the 2nd occurrence of the word “DATE”?

Many Thanks

Hi Folk,

As per my understanding you have a whole string something like this.
DATE asdfghjkl
DATE 02/05/2019 DAYTIME”

and you want to extract “02/05/2019”.

so there are 2 ways

  1. using regex where DATE is start word and ends with DAYTIME. (i am not very good in regex)
  2. another is as you said you need to substring after 2nd DATE occurance.
    so firstly you split the whole string on base of “DATE” and you will be getting array like
    {“”, “asdfghjkl”, “02/05/2019 DAYTIME”}. now at 2nd index replace the DAYTIME with " " or you can use substring or any logic to get the date.

hope your issue get resolved.

Happy coding…

I’m not sure if I understood problem, but this could be useful to get Dates inside a Text

Regex: [0-9]{2}/[0-9]{2}/[0-9]{4}