MQL4 text handling

MQL4 string functions with safe parsing and formatting

Use StringFormat to assemble structured messages, DoubleToString for price precision, StringFind to locate text, and StringSubstr only after checking the returned position. MQL4 strings are Unicode, and a backslash starts an escape sequence inside a literal.

Open the Coding Agent, then choose MQL4 from the language menu.

StringFunctionsExample.mq4.mq4
#property strict

void OnStart()
  {
   double price = 1.23456;
   string message = StringFormat("%s close=%s",
                                 Symbol(),
                                 DoubleToString(price, Digits));

   int marker = StringFind(message, "close=");
   if(marker < 0)
     {
      Print("Expected field was not found.");
      return;
     }

   string value = StringSubstr(message, marker + 6);
   Print("Message: ", message);
   Print("Parsed close: ", value);
  }
  • Format values explicitly
  • Check StringFind before slicing
  • Treat positions as character indexes
  • Escape literal backslashes

Choose conversion and formatting deliberately

StringFormat follows printf-style placeholders and is useful when a message contains several typed values. DoubleToString exposes the required decimal precision directly, which makes it clearer for price output than relying on an implicit conversion.

A formatted string is presentation data. Converting a number to text and back can lose precision if the chosen digit count was too small, so calculations should remain numeric until the output boundary.

FunctionUseCheck
StringFormatAssemble typed outputPlaceholder and argument types
DoubleToStringRender a doubleRequired digits
StringFindLocate a substringResult is not -1
StringSubstrExtract charactersStart and length stay in range

Parse only after validating delimiters

StringFind returns the zero-based position of the first match from the requested start position, or minus one when no match exists. Adding an offset to minus one can produce a plausible but wrong substring.

StringSplit is useful for delimiter-based records, but its returned field count still needs validation. External text can contain missing fields, extra separators, different line endings, or an unexpected encoding.

  • Check every search position before using StringSubstr
  • Check the field count returned by StringSplit
  • Trim or normalize input only when the data contract permits it
  • Keep numbers numeric until final display or transport

Backslashes and quotes are source syntax

Inside a string literal, backslash introduces escapes such as newline, tab, a quoted character, or a literal backslash. A Windows path therefore needs doubled backslashes in source code.

Escaping determines the characters produced by the compiler. It does not validate a file path, URL, JSON document, or broker message format after the string is created.

Review the complete example

The script formats a symbol and price, verifies that the field marker exists, and only then extracts the value. It avoids slicing from the negative value returned when StringFind cannot locate text.

The example only transforms local text. Validate any parsed value again before using it in file access, network requests, or trading decisions.

StringFunctionsExample.mq4.mq4
#property strict

void OnStart()
  {
   double price = 1.23456;
   string message = StringFormat("%s close=%s",
                                 Symbol(),
                                 DoubleToString(price, Digits));

   int marker = StringFind(message, "close=");
   if(marker < 0)
     {
      Print("Expected field was not found.");
      return;
     }

   string value = StringSubstr(message, marker + 6);
   Print("Message: ", message);
   Print("Parsed close: ", value);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Define the text contract

    List required fields, delimiters, precision, and missing-value behavior.

  2. 2

    Format typed values

    Use an explicit conversion or StringFormat placeholder for each value.

  3. 3

    Validate search results

    Reject missing delimiters before calculating substring positions.

  4. 4

    Test edge input

    Compile and test empty strings, Unicode text, extra separators, and escaped characters.

Frequently asked questions

How do I format a string in MQL4?

Use StringFormat with placeholders that match the argument types. Use DoubleToString when a double needs an explicit number of digits.

What does StringFind return when text is missing?

StringFind returns -1 when the requested substring cannot be found.

Are MQL4 string indexes byte positions?

String functions operate on character positions in MQL4 Unicode strings, not raw UTF-8 byte offsets.

How do I put a backslash in an MQL4 string?

Write two backslashes in the source literal to produce one literal backslash character.

Should prices be stored as strings?

No. Keep prices as doubles for calculations and convert them only for display, logging, or a defined transport format.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent