Search code examples
c#stringdatetime.net-micro-framework

find the index of the first number that appears after a certain substring in c#


Background Info

I have a fez panda II (that's running on netmf 4.1) in which is reading a load of bytes. These bytes are then being saved to an SD card in text format.

Currently, I'm saving the file as 1, 2, 3...

However, I need these to be timestamped instead.

Since the Panda will loose power regularly, I can't use the on-board clock, nor can it use the DateTime.Now property.

The bytes being read also contain a start time.

The format of the string looks similar to:

c5@!c5dcSTART 11/01/2015          12:49:12
more data.... 124 xyz
more data...
some more data...
some even more numeric/text data...
more data which include the word "START"...

The Goal

Is there a way of retrieving this start time? It will have to be pretty fast too, since the data is being read at some speed, and the file handling thread is already lagging slightly.

The date would also need to be saved as a file name, so the forward slashes may need to be replaced with "file name safe" alternatives.

My overall Goal is to timestamp the files as they are saved.

I'm thinking if I could get the first 12 digits after the first occurrence of START I should be able to work on from there.

Anyone know how i could get the index of '1' after the word 'START'?


Attempts To Date

I have tried using the String.IndexOf, then a substring to contain the 'line', followed by a trim, although this seems extremely slow and will more than likely cause a lose of data.

I have also tried splitting the whole message into a char array (and going through to find the word "START", and then going through to get the 'numbers' after it), but this also seems to be very inefficient


Updates

I've very recently found out that I can't use regex as this was only implemented for NetMF 4.2+ whereas the Fez Panda runs on 4.1.


Solution

  • executing on the first raw of data the following code, should produce desired values

     var splits = val.Split(new char[]{' ', '\t'}`)
     string date = splits[1];
     string time = splits[2];