Hi folks,
Our set of functions today are the String Functions. They are the MQL4 functions we need to handle the string variables!
Let's give the string data type an overview first!
The string data type is an array of characters enclosed in double quote (").
This array of characters is an array which holds one character after another, starting at index 0. After the last character of data, a NULL character is placed in the next array location. It does not matter if there are unused array locations after that.
A NULL character is a special character (represented by the ASCII code 0) used to mark the end of this type of string.
See figure 1 for a simple representation of the string constant “hello” in the characters array.
MQL4 limits the size of the string variable to 255 characters and any character above 255 characters will generate this error: (too long string (255 characters maximum)).
We use the keyword string to create a string variable.
For example:
| string str1 = "Hello world!, with you coders guru”; |
Now let's study the MQL4 functions concerned about the Strings:
Syntax:
| string StringConcatenate( ... ) |
Description:
The StringConcatenate function take the set of parameters passed to it and glue them together and returns them as a string.
Note: (...) means that you can pass to this function any kind of data type and any number of parameters separated by comma.
You can pass to this function any kind of data type except the arrays and any number of parameters separated by comma very like the Print(), Alert() and Comment() functions.
If you want to pass a double data type to this function the number of digits will be used are 4 digits if you want to change this number you have to use the function DoubleToStr().
The bool, color and datatime passed to function will be printed in their numeric presentation. , if you want to print them in string format use the string name of the bool or the color value and for the datetime use the function TimeToStr().
Note: You can use the + operator to add string to another string for example "coders" + "guru" will produce "codersguru", however MetaTrader saying that StringConcatenate() function works faster gluing strings together.
Parameters:
This function takes any number of parameters:
...
Any values, separated by commas.
Example:
| string text; |
Syntax:
| int StringFind( string text, string matched_text, int start=0) |
Description:
The StringFind function search for substring (part of the given string for example "gur" is substring from the string "codersguru") in a given string, if the function find the substring it returns its position, if the function didn't find the substring it returns -1.
Parameters:
This function takes two parameters:
string text
The string you want to search in it for the substring.
string matched_text
The substring you want to find it in the string.
int start=0
The index of the starting position for the search. the first character in the string has the index 0 then 1 then 2 etc.
The default value is 0 which means to search from the start of the string.
Example:
| string text="The quick brown dog jumps over the lazy fox"; |
Syntax:
| int StringGetChar( string text, int pos) |
Description:
The StringGetChar function returns the character in a given position from a given string. it return it as character code (the ASCII code of the character).
Parameters:
This function takes two parameters:
string text
The string you want to get the character from.
int pos
The position of the character you want to get, don't forget that the string array starts with 0.
Example:
| int char_code=StringGetChar("abcdefgh", 3); |
Syntax:
| int StringLen( string text) |
Description:
The StringLen function returns how many character (the length) of the given string.
Parameters:
This function takes only one parameter:
string text
The string you want to get its length..
Example:
| string str="some text"; |
Syntax:
| string StringSetChar( string text, int pos, int value) |
Description:
The StringSetChar function change one character of the given string in a given position to another character and returns the new string.
Parameters:
This function takes three parameters:
string text
The string you want change a character in it.
int pos
The position of the character you want to change.
int value
The new character you want to set it.
Example:
| string str="abcdefgh"; |
Syntax:
| string StringSubstr( string text, int start, int count=EMPTY) |
Description:
The StringSubstr function extracts the string from given position to a given count (ex: "codersguru" the position is 3 and the count is 2 then the extracted string is "de"). The function returns the extracted string if any or it returns EMPTY string.
Parameters:
This function takes three parameters:
string text
The string you want to extract from it.
int start
Where to start your extracting.
int count=EMPTY
The count of character you want to extract. The default value is EMPTY.
Example:
| string text="The quick brown dog jumps over the lazy fox"; |