1

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 Answers 4

2

You are not accounting for the length of firstTag:

int t = f.IndexOf(firstTag, index) + firstTag.Length; 
Sign up to request clarification or add additional context in comments.

1 Comment

Ed this is working indeed: int index = 0; int t = f.IndexOf(firstTag, index) + firstTag.Length; int g = f.IndexOf(lastTag, index); string a = f.Substring(t, g - t);
0

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); 

1 Comment

Zong Zheng i did : DateTime dt = DateTime.ParseExact(a, "yyyyMMddhhmmss", CultureInfo.InvariantCulture); The variable a now contain 201309202215 but im getting an error/exception: FormatException: String was not recognized as a valid DateTime
0

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.

1 Comment

Omada using your line now the variable a contain: 201309202215&ir=true","/image2.ashx?reg
0

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.