1
votes

In Form1 I have:

satelliteMapToRead = File.ReadAllText(localFilename + "satelliteMap.txt");

Then in the constructor:

ExtractImages.ExtractDateTime("image2.ashx?region=eu&time=", "&ir=true", satelliteMapToRead);

Then in the ExtractImages class i have:

public static void ExtractDateTime(string firstTag, string lastTag, string f)
{
    int index = 0;
    int t = f.IndexOf(firstTag, index);
    int g = f.IndexOf(lastTag, index);
    string a = f.Substring(t, g - t);
}

This an example of a string in the text file:

image2.ashx?region=eu&time=201309202145&ir=true

From this string i want that the variable g will contain only: 201309202145 And then to convert the variable a to date time : date 2013 09 20 - time 21 45

What I get in the variable a now is:

image2.ashx?region=eu&time=201309202215

And it's not what I need.

4

4 Answers

2
votes

You are not accounting for the length of firstTag:

int t = f.IndexOf(firstTag, index) + firstTag.Length;
0
votes

Since you're already doing it this way, juts use indexOf("time=") again to get 201309202215. Then the date/time is given by

DateTime.ParseExact(str, "yyyyMMddhhmmss", CultureInfo.InvariantCulture);
0
votes

Switch this line to:

int t = f.IndexOf(firstTag, index) + firstTag.Length;

IndexOf returns the position of the first character of your string. So in your example, t is actually zero. This is why a also has the first tag in it.

0
votes

Without error handling (missing parameters or invalid format):

string url = "image2.ashx?region=eu&time=201309202145&ir=true";
var queryString = System.Web.HttpUtility.ParseQueryString(url);
DateTime dt = DateTime.ParseExact(queryString["time"], "yyyyMMddHHmm", CultureInfo.InvariantCulture);