removing text from a string

 

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 

 
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 .

 

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

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

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.   

 
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 */
 
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!
 
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.

Reason: