How to Extract from a String

Back to topics list To post a new topic, please log in or register
avatar
83
barnacle7 2015.04.23 14:51
 

    

I have date in CSV  file with the Date formatted as:  month /day /year .

How can I extract the  month / day / year  ( 04/ 23/ 2015 )  from the String ?

I can't use anything that converts it to Datetime otherwise it becomes day/month/year format.

 

Thanks 

 

avatar
Moderator
3427
GumRai 2015.04.24 00:01 #
 

I'm not really sure what you want

 EDIT: I don't know what goes on - Sometimes I include SRC and when I post, it has disappeared

avatar
Moderator
3427
GumRai 2015.04.24 00:02 #
 
  string source="4/23/2015";
  string temp_array[];
  ushort sep=StringGetCharacter("/",0);
  StringSplit(source,sep,temp_array);
  int day=StrToInteger(temp_array[1]);
  int month=StrToInteger(temp_array[0]);
  int year=StrToInteger(temp_array[2]);
  Print("Month=",month,", Day=",day,", Year=",year);
  datetime date=StringToTime(temp_array[2]+"."+temp_array[0]+"."+temp_array[1]);
  Print(date);

avatar
736
honest_knave 2015.04.24 02:17 #
 
GumRai:

I'm not really sure what you want

 EDIT: I don't know what goes on - Sometimes I include SRC and when I post, it has disappeared

It happens with links and images to.

It seems to happen if inserting code/link/image is the last thing you do before submitting.

Consequently I now hit enter a few times before submit! 


avatar
13010
WHRoeder 2015.04.24 14:55 #
 
barnacle7:

I have date in CSV file with the Date formatted as: month /day /year .

How can I extract the  month / day / year  ( 04/ 23/ 2015 )  from the String ?

I can't use anything that converts it to Datetime otherwise it becomes day/month/year format.

  1. You can't use the built in StrToTime - MQL4 Documentation to create a datetime because it expects ""yyyy.mm.dd hh:mi" So you reformat your string
  2. If the string has spaces (as you posted) trim them after splitting

Not compiled, not tested.

string MDY_To_YMD(const string& mdy, ushort sep='/'){     // "month/day/year"
  string split[]; if(3 != StringSplit(mdy, sep, split) ) return 0;

  int year = StrToInteger(split[2]);
  if(year < 100) year += year < 50 ? 2000 : 1900;           // 99->1999 01->2001

  return StringFormat("%i.%s.%s" year, split[0], split[1]); // "Year.Month.day"
}
datetime MdyToTime(const string& mdy, ushort sep='/'){
  return StringToTime( MDY_To_YMD(ymd, sep) );
}
string   ymd  = "4/25/15";
datetime when = MdyToTime(ymd);

Not compiled, not tested.


Back to topics list  

To add comments, please log in or register