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.gifNew: 0248240420428420842
Old: 0824420.gifNew: 0824420
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 .
another is StringReplace()
string text="The quick brown fox jumped over the lazy dog.";
Print("Replaced: ", replaced,". Result=",text);
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.
... yet another is:
StringSplit(x, '.', arr);
// Desired part of string is now in arr
... 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":
y = StringSubstr(x, 0, StringLen(x) - StringLen(arr[StringSplit(x, '.', arr) - 1]) - 1);
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:
I used the line:
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.
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 */
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.