Download MetaTrader 5
To add comments, please log in or register
Interested in earning money via MQL5 Cloud Network? Enter your profile!
trader88888
131
trader88888 2014.11.05 16:38 

Hello,

 

New to mql4 and really coding in general but I have to start some where.  Can anyone help me with this?  I have a random string of numbers (different character lengths) that end with .gif.   how do I remove just the ".gif" from a string?

 

Old:   0248240420428420842.gif
New:  0248240420428420842

 

example 2:

Old:  0824420.gif
New: 0824420 

 

if you could show me the syntax for the exact method that would be even more amazing.  thank you 

JC
1395
JC 2014.11.05 16:44  
trader88888:

if you could show me the syntax for the exact method that would be even more amazing.  thank you 

 There are many ways of doing it, but one of them is:

StringSubstr(x, 0, StringFind(x, "."));

The use of StringFind looks for the first . character in the string, and StringSubstr returns the original string from position 0 up to, but not including, the position of the .

rod178
276
rod178 2014.11.05 16:47  

another is StringReplace()

eg

  string text="The quick brown fox jumped over the lazy dog.";
  int replaced=StringReplace(text,"quick","slow");
  replaced+=StringReplace(text,"brown","black");
  replaced+=StringReplace(text,"fox","bear");
  Print("Replaced: ", replaced,". Result=",text);
JC
1395
JC 2014.11.05 16:56  
rod178:
another is StringReplace()

Indeed. Another would be StringSubstr(x, 0, StringLen(x) - 4). But StringReplace will contain an assumption that the extension is always .gif, and never .jpg, .png etc. Use of StringLen contains an assumption that the extension will be .gif, .jpg etc but not .jpeg

The StringFind method wouldn't work if the filenames were capable of being in the form firstpart.secondpart.extension, e.g. EURUSD.0234234234.gif. It would be preferable to use something equivalent to the LastIndexOf() which is available in most programming languages. But MQL4 doesn't have one. 

JC
1395
JC 2014.11.05 16:59  
jjc:

 There are many ways of doing it, but one of them is:

... yet another is:

string arr[];
StringSplit(x, '.', arr);
// Desired part of string is now in arr[0]
Carl Schreiber
5962
Carl Schreiber 2014.11.05 17:00  
JC
1395
JC 2014.11.05 17:28  
jjc:

... yet another is:

... and the rococo way of doing it is as follows, which in effect does a last-index-of, and returns "firstpart.secondpart" if the original filename is "firstpart.secondpart.extension":

string arr[];
y = StringSubstr(x, 0, StringLen(x) - StringLen(arr[StringSplit(x, '.', arr) - 1]) - 1);
trader88888
131
trader88888 2014.11.05 17:48  

Thanks all for contributing.     The file assumption of .gif is ok in this case (although I do see your point) so I tried to start with something simple.

 

The string for the variable fileName was:

RUSS2000-1415207148.gif

 I used the line:  

fileName2=StringReplace(fileName,".gif","");

  The result after of FileName2 was:     1

 I was assuming it would just remove the .gif and replace it with nothing.   Not sure where this "1" came from and what I had done wrong?  

 

Thanks guys!!!! appreciated.   

JC
1395
JC 2014.11.05 17:51  
trader88888:

 I was assuming it would just remove the .gif and replace it with nothing.   Not sure where this "1" came from and what I had done wrong?  

The MT4 string functions aren't consistent. StringSubstr() returns the modified string. You are assuming that StringReplace() works the same way, but it doesn't. The first parameter is an in/out string variable, and the return value is the number of replacements made. Your code would need to be:

string fileName2 = fileName; /* Take a copy which is then modified by StringReplace() */
StringReplace(fileName2, ".gif", ""); /* filename2 is modified in place; it is not the function return value */
trader88888
131
trader88888 2014.11.05 18:02  
jjc:

The MT4 string functions aren't consistent. StringSubstr() returns the modified string. You are assuming that StringReplace() works the same way, but it doesn't. The first parameter is an in/out string variable, and the return value is the number of replacements made. Your code would need to be:

 

It worked!! Thank you for the details, it will probably help me with a million other things :)  thanks!
JC
1395
JC 2014.11.05 18:06  
trader88888:
It worked!! Thank you for the details, it will probably help me with a million other things :)  thanks!

BTW, your assumption about StringReplace() was incorrect but perfectly reasonable. Every other programming language I can think of works that way.

MT5 isn't consistent: StringSubstr() returns the modified string, but all other manipulation functions such as StringReplace(), StringTrimLeft(), StringToUpper() etc modify their strings in place. When the MQL4 language was expanded in MT4 build 600, it got the MT5 syntax for the new string functions. But StringConcatenate() already existed in MT4, and therefore MT4 and MT5 have different syntax for StringConcatenate(). It's all a mess.

/
To add comments, please log in or register