Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

String operation functions - MetaTrader 5 library | MT5 EA download - MetaTrader 5 resources

author EAcpu | 5 reads | 0 comments |

A collection of useful string manipulation functions

All functions in the library will return a modified copy of the source string.

The source string is unmodified (i.e. immutable).

//+------------------------------------------------------------------+
//| StringUtils_Demo|
//| Copyright © 2018, Eminem Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#Property Copyright "Copyright © 2018, Eminem Ali"
#Property association "https://www.mql5.com/en/users/amrali"
# Property version "1.800"
# Property description "A collection of string manipulation functions."

#include //+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define print (A) print assistant ( #a, (a)) //+----------------------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Template < type name > Blank Print Assistant ( string_one , constant TA)
  {
if ( typename (T)== "string" )
Print (_A+ ”=" , “\”” + ( thin string )A+ “\”” );
Other print (_A+ ”=" , ( string ) a);
  } //+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
blank start ()
  {
/**
    * All functions return a modified copy of the input string.
    * The source string is not modified (i.e. immutable).
    */ print (string append if missing ( "directory" , "\\" ) );
  print (string append if missing ( "directory" , "\\" ) );
  print(String char( "apple" , 4 ));
  print(StringCharCode( "apple" , 4 ));
string part[];
  print(StringBlock( "1234567890" , 3 , part));
arrayprint (part);
  print(StringChunkRight( "1234567890" , 3 , part));
arrayprint (part);
  PRINT(String contains( "Life is good" , "Yes" ));
  print(string countmatch( "Mr. Blue has a blue house and a blue car" , "blue" ));
  print(String end( "Life is good" , "Okay" ));
  print(String end( "Life is good" , "Yes" , 7 ));
  print(string generate random number( 12 ));
  print(String index( "morning" , "n" ));
  PRINT(String insert( "012345" , "xxx" , 3 ));
  PRINT(String is empty or empty ( invalid ));
  PRINT(String is empty or empty( "" ));
  PRINT(string is number( "12345" ));
  PRINT(string is number( "3.142" ));
  PRINT(String concatenation( "-" , "java" , "is" , "cool"); Print(String PadEnd( "1.3" , 5, '0' )); Print(String PadStart( "USD " , 5 )); Print(String PadStart( "123" , 5 , '0' )); PRINT(StringPrependIfMissing( "Domain name.com" , "World Wide Web.")); PRINT(StringPrependIfMissing( " www.domain.com " . String Repeat( "*" , 5 ) ); PRINT(String Repeat( ' *', 5 ) ) ; PRINT(String Replacement 2( "Mr. Blue has a blue house and a blue car" , " Blue", "Red" ) ); PRINT(String Replacement Between ("foo " , "" , "", "Bar" )); PRINT(String Reverse( "012345 ")); print(String right( "Helicopter" )); print(String right( "vehicle" , 2 )); print(String right(" car " , 5 )); print(String shuffle(" 012345 ")); print(String split("_ life is_good_ " , " _" , part )); array print(part); print(string split trim( " _life is_good_", "_" , part ) ); Array printing ( part ); PRINT(String start( "Life is good ", "life" ) ); PRINT( String start( "Life is good" , "Yes" , 5 )); PRINT(StringSubstrAfter ( "ABCBA" , "b" )); PRINT(StringSubstrAfterLast ("ABCBA", "b") )); PRINT(StringSubstrBefore( "ABCBA" , " b" ) ); PRINT( StringSubstrBeforeLast( "ABCBA" , "b" ) ); PRINT( StringSubstrBetween( " foo " , " " , " " ) ); PRINT( String to lowercase ( "MetaTrader 5" ) ); PRINT( String to uppercase ( " MetaTrader 5" ) ); print(String trimmed end( " Hello world!")); print(String trimmed start("Hello world!")); print(DQuoteStr(MQL message string(MQL_program_path)))); print(StrHashCode("https://twitter.com/") ) ) ; print (StrHashCode (" Oops! ")); constant long magic number = (( long ) StrHashCode( " My Expert Name" ) << 31 ) + StrHashCode( _symbol ); print(magic number); print(Base64 encoded( "https://twitter.com/ " )); print(Base64 decoded( "aHR0chHM6Ly90d2l0dGVyLmNvbS8=") )); print(Base64 encoded( "Oops!" )); Print(Base64 decode( "0J/RgNC40LLQtdGCINC80LjRgCE=" )); Uchar Bytes[]; Print(UTF8GetBytes( "MQL5" , bytes)); <span class="functions">Array printing (bytes); Print (UTF8GetString (bytes)); Ultra short characters []; Print ( UnicodeGetBytes ( "MQL5" , characters) ); Array printing ( characters); PRINT ( UnicodeGetString(chars) ); } //+--------------------------------------------------------------------------------+ // Expected output: /* StringAppendIfMissing(dir,\) = "dir\" StringAppendIfMissing(dir\,\) = "dir\" StringCharAt(apple,4) = "e" StringCharCodeAt(apple,4) = 101 StringChunk(1234567890,3,part) = 4 "123" "456" "789" "0" StringChunkRight(1234567890,3,part) = 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 String insertion(012345,xxx,3) = "012xxx345" StringIsNullOrEmpty(NULL) = true StringIsNullOrEmpty() = true StringIsNumeric(12345) = true StringIsNumeric(3.142) = true StringJoin(-,Java,is,cool) = "Java is cool" StringLastIndexOf(am,n) = 5 StringLeft(helicopter) = "h" StringLeft(vehicle,2) = "ve" StringLeft(car,5) = "car" StringPad(MQL5 great,21,'*') = "***MQL5 great***" 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" String Repeat(*,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( foo , , ,bar) = " bar " String Reverse (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( foo , , ) = "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!" StrHashCode(https://twitter.com/) = 2363652379 StrHashCode(Привет мир!) = 3271322339 Magic Number = 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, characters) = 4 77 81 76 53 UnicodeGetString(chars) = "MQL5" */





Attachment download

📎 stringutils.mqh (85.33 KB)

📎 stringutils_demo.mq5 (16.91 KB)

Source: MQL5 #35395

Verification code Refresh