Download MetaTrader 5

Looking for 'Split String' - String Function

To add comments, please log in or register
Lime
649
Lime  

Hi Programmers!


I'm looking for a string function or a methode can help me.


I would like the Symbol() string to split into two string. For example if the Symbol() is 'EURUSD', I would like to get String_1 = 'EUR' and String_2 = 'USD'


I would be very happy if you could help me.


Thank you in advance.

jacques366
336
jacques366  

Hi,

The solution is the instruction : StringSubstr

String_1 = StringSubstr(Symbol(), 0, 3);

String_2 = StringSubstr(Symbol(), 3, 3);

Cheers

Lime
649
Lime  

Thank you very much Jacques!


I was regardless when I was looking over the String Functions.

Miroslav Popov
393
Miroslav Popov  
//+------------------------------------------------------------------+  
//+------------------------------------------------------------------+
// Helper function for parsing command strings, results resides in g_parsedString.
bool SplitString(string stringValue, string separatorSymbol, string& results[], int expectedResultCount = 0)
{
//	 Alert("--SplitString--");
//	 Alert(stringValue);

   if (StringFind(stringValue, separatorSymbol) < 0)
   {// No separators found, the entire string is the result.
      ArrayResize(results, 1);
      results[0] = stringValue;
   }
   else
   {   
      int separatorPos = 0;
      int newSeparatorPos = 0;
      int size = 0;

      while(newSeparatorPos > -1)
      {
         size = size + 1;
         newSeparatorPos = StringFind(stringValue, separatorSymbol, separatorPos);
         
         ArrayResize(results, size);
         if (newSeparatorPos > -1)
         {
            if (newSeparatorPos - separatorPos > 0)
            {  // Evade filling empty positions, since 0 size is considered by the StringSubstr as entire string to the end.
               results[size-1] = StringSubstr(stringValue, separatorPos, newSeparatorPos - separatorPos);
            }
         }
         else
         {  // Reached final element.
            results[size-1] = StringSubstr(stringValue, separatorPos, 0);
         }
         
         
         //Alert(results[size-1]);
         separatorPos = newSeparatorPos + 1;
      }
   }   
   
   if (expectedResultCount == 0 || expectedResultCount == ArraySize(results))
   {  // Results OK.
      return (true);
   }
   else
   {  // Results are WRONG.
      Trace("ERROR - size of parsed string not expected.", true);
      return (false);
   }
}
Source: http://www.openforexplatform.com/
Iggy
478
Iggy  

Here is a function from my library of commonly used functions:

//+------------------------------------------------------------------+
//| find n-th substring based on separator 
//+------------------------------------------------------------------+
string StringSplit(string input,string sep, int index)
{
int count=0;
int oldpos=0;
int pos=StringFind(input,sep,0);
while(pos>=0&&count<=index)
{
if(count==index)
{
if(pos==oldpos)
{
return("");
}
else
{
return(StringSubstr(input,oldpos,pos-oldpos));
}
}
oldpos=pos+StringLen(sep);
pos=StringFind(input,sep,oldpos);
count++;
}
if(count==index)
{
return(StringSubstr(input,oldpos));
}
return("");
}
tabs were lost.
Lime
649
Lime  
Thank you for the functions Miroslav Popov and Irusoh!
Ulterior
84
Ulterior  
Thank you for the functions
AtApi
75
AtApi  
Thanks guys really appreciated!
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
AtApi:
Thanks guys really appreciated!
Your thanks are 3 years late . . .   why are you dredging up a very old thread ?
Rosen Marinchev
139
Rosen Marinchev  
Why are you not locking the topic then ?
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
moreevilthanyou:
Why are you not locking the topic then ?
How ?  there is no facility to lock a topic.
12
To add comments, please log in or register