A collection of useful string manipulation functions
Function | Description |
---|---|
StringAppendIfMissing()
|
Appends the suffix to the end of the string if the string does not already end with the suffix. |
StringCharAt()
|
Returns the character at the specified index in a string. |
StringCharCodeAt()
|
Returns a short integer between 0 and 65535 representing the UTF-16 code unit at the given index. |
StringChunk()
|
Split a string into evenly sized chunks of the specified length. |
StringChunkRight()
|
Split a string into evenly sized chunks of the specified length, starting the split at the end of the string. |
StringContains()
|
Determines whether this string contains the specified substring. |
StringCountMatches()
|
Counts the number of all occurrences of the specified string in the input string using case-sensitive search. |
StringEndsWith()
|
Determines whether this string ends with the specified suffix. |
StringGenerateRandom()
|
Generates a random string with a desired length from ascii characters. |
StringIndexOf()
|
Returns the index within this string of the first occurrence of the specified substring, starting the search at the specified index. |
StringInsert()
|
Returns a new string in which a specified substring is inserted at a specified index position in this string. |
StringIsNullOrEmpty()
|
Indicates whether the specified string is NULL or an empty string (“”). |
StringIsNumeric()
|
Determines whether the input string consists only of decimal digits. |
StringJoin()
|
Returns the result string, formed by joining of string parameters using a specified separator. |
StringLastIndexOf()
|
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. |
StringLeft()
|
Extracts the leftmost ‘length’ characters of a string. |
StringPad()
|
Returns a new string of a specified length in which the input string is centered by padding with a specified character. |
StringPadEnd()
|
Returns a new string of a specified length in which the end of the input string is padded with a specified character. |
StringPadStart()
|
Returns a new string of a specified length in which the beginning of the input string is padded with a specified character. |
StringPrependIfMissing()
|
Prepends the prefix to the start of the string if the string does not already start with the prefix. |
StringRemove()
|
Returns a new string in which all occurrences of a specified string in the input string are removed. |
StringRemoveEnd()
|
Removes the specified suffix from the end of the string if the string already ends with the suffix. |
StringRemoveStart()
|
Removes the specified prefix from the start of the string if the string already starts with the prefix. |
StringRepeat()
|
Returns a new string which contains the specified number of copies of the input string, concatenated together. |
StringReplace2()
|
Returns a new string in which all occurrences of a specified string in the input string are replaced with another specified string. |
StringReplaceBetween()
|
Replaces the string that is nested in between two string tags. Only the first match is replaced. |
StringReverse()
|
Returns a copy of this string with all the characters reversed. |
StringRight()
|
Extracts the rightmost ‘length’ characters of a string. |
StringShuffle()
|
Returns a copy of this string with all the characters shuffled. |
StringSplit()
|
Overload for string separator. Split a string into an array of substrings, using a specified separator string. |
StringSplitTrim()
|
As StringSplit + Substrings that consist only of white-space characters are removed from the result. |
StringStartsWith()
|
Determines whether this string starts with the specified prefix. |
StringSubstrAfter()
|
Gets the substring after the first occurrence of a separator. The separator is not returned. |
StringSubstrAfterLast()
|
Gets the substring after the last occurrence of a separator. The separator is not returned. |
StringSubstrBefore()
|
Gets the substring before the first occurrence of a separator. The separator is not returned. |
StringSubstrBeforeLast()
|
Gets the substring before the last occurrence of a separator. The separator is not returned. |
StringSubstrBetween()
|
Gets the string that is nested in between two string tags. Only the first match is returned. |
StringToLowerCase()
|
Returns a copy of this string converted to lowercase. |
StringToUpperCase()
|
Returns a copy of this string converted to uppercase. |
StringTrim()
|
Returns a new string in which all leading and trailing white-space characters found in the input string are removed. |
StringTrimEnd()
|
Returns a new string in which all trailing white-space characters found in the input string are removed. |
StringTrimStart()
|
Returns a new string in which all leading white-space characters found in the input string are removed. |
DQuoteStr()
|
Returns a copy of this string enclosed in a pair of double quotes. This helps to pass string parameters on the command-line. |
StrHashCode()
|
Generates 32 bit FNV-1a hash value from the given string. |
Base64Encode()
|
Encodes a string using Base64 encoding scheme. |
Base64Decode()
|
Decodes a Base64-encoded string into the original string. |
UTF8GetBytes()
|
Encodes this string into a sequence of bytes using UTF-8 encoding. This helps to send string messages via web sockets. |
UTF8GetString()
|
Decodes a sequence of bytes into a string using UTF-8 encoding. |
UnicodeGetBytes()
|
Encodes this string into a sequence of bytes using Unicode (UTF16) encoding. This helps to pass string parameters to Windows api functions. |
UnicodeGetString()
|
Decodes a sequence of bytes into a string using Unicode (UTF16) encoding. |
Immuable strings:
All functions in the library will return a modified copy of the source string.
The source string is not modified (i.e., immutable).
Demo
//+------------------------------------------------------------------+ //|                                                StringUtils_demo | //|                                        Copyright © 2018, Amr Ali | //|                            https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #property copyright "Copyright © 2018, Amr Ali" #property link      "https://www.mql5.com/en/users/amrali" #property version  "1.800" #property description "A collection of string manipulation functions." #include <StringUtils.mqh> //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ #define PRINT(A) PrintHelper(#A, (A)) //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ template<typename T> void PrintHelper(string _A, const T A)   {   if(typename(T) == "string")       Print(_A + " = ", """ + (string)A + """);   else       Print(_A + " = ", (string)A);   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ void OnStart()   {   /**     * All functions return a modified copy of the input string.     * The source string is not modified (i.e., immutable).     */   PRINT( StringAppendIfMissing("dir", "\") );   PRINT( StringAppendIfMissing("dir\", "\") );   PRINT( StringCharAt("Apple", 4) );   PRINT( StringCharCodeAt("Apple", 4) );   string parts[];   PRINT( StringChunk("1234567890", 3, parts) );   ArrayPrint(parts);   PRINT( StringChunkRight("1234567890", 3, parts) );   ArrayPrint(parts);   PRINT( StringContains("life_is_good", "is") );   PRINT( StringCountMatches("Mr Blue has a blue house and a blue car", "blue") );   PRINT( StringEndsWith("life_is_good", "good") );   PRINT( StringEndsWith("life_is_good", "is", 7) );   PRINT( StringGenerateRandom(12) );   PRINT( StringIndexOf("Morning", "n") );   PRINT( StringInsert("012345", "xxx", 3) );   PRINT( StringIsNullOrEmpty(NULL) );   PRINT( StringIsNullOrEmpty("") );   PRINT( StringIsNumeric("12345") );   PRINT( StringIsNumeric("3.142") );   PRINT( StringJoin("-", "Java", "is", "cool") );   PRINT( StringLastIndexOf("Morning", "n") );   PRINT( StringLeft("helicopter") );   PRINT( StringLeft("vehicle", 2) );   PRINT( StringLeft("car", 5) );   PRINT( StringPad("MQL5 is awesome", 21, '*') );   PRINT( StringPadEnd("USD", 5) );   PRINT( StringPadEnd("1.3", 5, '0') );   PRINT( StringPadStart("USD", 5) );   PRINT( StringPadStart("123", 5, '0') );   PRINT( StringPrependIfMissing("domain.com", "www.") );   PRINT( StringPrependIfMissing("www.domain.com", "www.") );   PRINT( StringRemove("Mr Blue has a blue house and a blue car", "blue ") );   PRINT( StringRemoveEnd("www.domain.com", ".com") );   PRINT( StringRemoveStart("www.domain.com", "www.") );   PRINT( StringRemoveStart("domain.com", "www.") );   PRINT( StringRepeat("*", 5) );   PRINT( StringRepeat('*', 5) );   PRINT( StringReplace2("Mr Blue has a blue house and a blue car", "blue", "red") );   PRINT( StringReplaceBetween("<a>foo</a>", "<a>", "</a>", "bar") );   PRINT( StringReverse("012345") );   PRINT( StringRight("helicopter") );   PRINT( StringRight("vehicle", 2) );   PRINT( StringRight("car", 5) );   PRINT( StringShuffle("012345") );   PRINT( StringSplit("_life_is_good_", "_", parts) );   ArrayPrint(parts);   PRINT( StringSplitTrim("_life_is_good_", "_", parts) );   ArrayPrint(parts);   PRINT( StringStartsWith("life_is_good", "life") );   PRINT( StringStartsWith("life_is_good", "is", 5) );   PRINT( StringSubstrAfter("abcba", "b") );   PRINT( StringSubstrAfterLast("abcba", "b") );   PRINT( StringSubstrBefore("abcba", "b") );   PRINT( StringSubstrBeforeLast("abcba", "b") );   PRINT( StringSubstrBetween("<a>foo</a>", "<a>", "</a>") );   PRINT( StringToLowerCase("MetaTrader 5") );   PRINT( StringToUpperCase("MetaTrader 5") );   PRINT( StringTrim("  Hello World!  ") );   PRINT( StringTrimEnd("  Hello World!  ") );   PRINT( StringTrimStart("  Hello World!  ") );   PRINT( DQuoteStr(MQLInfoString(MQL_PROGRAM_PATH)) );   PRINT( StrHashCode("https://twitter.com/") );   PRINT( StrHashCode("Привет мир!") );   const long magicNumber = ((long) StrHashCode("MyExpertName") << 31) + StrHashCode(_Symbol);   PRINT( magicNumber );   PRINT( Base64Encode("https://twitter.com/") );   PRINT( Base64Decode("aHR0cHM6Ly90d2l0dGVyLmNvbS8=") );   PRINT( Base64Encode("Привет мир!") );   PRINT( Base64Decode("0J/RgNC40LLQtdGCINC80LjRgCE=") );   uchar bytes[];   PRINT( UTF8GetBytes("MQL5", bytes) );   ArrayPrint(bytes);   PRINT( UTF8GetString(bytes) );   ushort chars[];   PRINT( UnicodeGetBytes("MQL5", chars) );   ArrayPrint(chars);   PRINT( UnicodeGetString(chars) );   } //+------------------------------------------------------------------+ // Expected output: /*   StringAppendIfMissing(dir,) = "dir"   StringAppendIfMissing(dir,) = "dir"   StringCharAt(Apple,4) = "e"   StringCharCodeAt(Apple,4) = 101   StringChunk(1234567890,3,parts) = 4   "123" "456" "789" "0"   StringChunkRight(1234567890,3,parts) = 4   "890" "567" "234" "1"   StringContains(life_is_good,is) = true   StringCountMatches(Mr Blue has a blue house and a blue car,blue) = 2   StringEndsWith(life_is_good,good) = true   StringEndsWith(life_is_good,is,7) = true   StringGenerateRandom(12) = "9VPP7jZcShNz"   StringIndexOf(Morning,n) = 3   StringInsert(012345,xxx,3) = "012xxx345"   StringIsNullOrEmpty(NULL) = true   StringIsNullOrEmpty() = true   StringIsNumeric(12345) = true   StringIsNumeric(3.142) = true   StringJoin(-,Java,is,cool) = "Java-is-cool"   StringLastIndexOf(Morning,n) = 5   StringLeft(helicopter) = "h"   StringLeft(vehicle,2) = "ve"   StringLeft(car,5) = "car"   StringPad(MQL5 is awesome,21,'*') = "***MQL5 is awesome***"   StringPadEnd(USD,5) = "USD  "   StringPadEnd(1.3,5,'0') = "1.300"   StringPadStart(USD,5) = "  USD"   StringPadStart(123,5,'0') = "00123"   StringPrependIfMissing(domain.com,www.) = "www.domain.com"   StringPrependIfMissing(www.domain.com,www.) = "www.domain.com"   StringRemove(Mr Blue has a blue house and a blue car,blue ) = "Mr Blue has a house and a car"   StringRemoveEnd(www.domain.com,.com) = "www.domain"   StringRemoveStart(www.domain.com,www.) = "domain.com"   StringRemoveStart(domain.com,www.) = "domain.com"   StringRepeat(*,5) = "*****"   StringRepeat('*',5) = "*****"   StringReplace2(Mr Blue has a blue house and a blue car,blue,red) = "Mr Blue has a red house and a red car"   StringReplaceBetween(<a>foo</a>,<a>,</a>,bar) = "<a>bar</a>"   StringReverse(012345) = "543210"   StringRight(helicopter) = "r"   StringRight(vehicle,2) = "le"   StringRight(car,5) = "car"   StringShuffle(012345) = "145230"   StringSplit(_life_is_good_,_,parts) = 5   ""    "life" "is"  "good" ""   StringSplitTrim(_life_is_good_,_,parts) = 3   "life" "is"  "good"   StringStartsWith(life_is_good,life) = true   StringStartsWith(life_is_good,is,5) = true   StringSubstrAfter(abcba,b) = "cba"   StringSubstrAfterLast(abcba,b) = "a"   StringSubstrBefore(abcba,b) = "a"   StringSubstrBeforeLast(abcba,b) = "abc"   StringSubstrBetween(<a>foo</a>,<a>,</a>) = "foo"   StringToLowerCase(MetaTrader 5) = "metatrader 5"   StringToUpperCase(MetaTrader 5) = "METATRADER 5"   StringTrim(  Hello World!  ) = "Hello World!"   StringTrimEnd(  Hello World!  ) = "  Hello World!"   StringTrimStart(  Hello World!  ) = "Hello World!  "   DQuoteStr(MQLInfoString(MQL_PROGRAM_PATH)) = ""C:Program FilesMetaTrader 5MQL5ScriptsStringUtils_demo.ex5""   StrHashCode(https://twitter.com/) = 2363652379   StrHashCode(Привет мир!) = 3271322339   magicNumber = 9094825662509768225   Base64Encode(https://twitter.com/) = "aHR0cHM6Ly90d2l0dGVyLmNvbS8="   Base64Decode(aHR0cHM6Ly90d2l0dGVyLmNvbS8=) = "https://twitter.com/"   Base64Encode(Привет мир!) = "0J/RgNC40LLQtdGCINC80LjRgCE="   Base64Decode(0J/RgNC40LLQtdGCINC80LjRgCE=) = "Привет мир!"   UTF8GetBytes(MQL5,bytes) = 4   77 81 76 53   UTF8GetString(bytes) = "MQL5"   UnicodeGetBytes(MQL5,chars) = 4   77 81 76 53   UnicodeGetString(chars) = "MQL5" */