Download MetaTrader 5

How to search an array with string values?

To add comments, please log in or register
Do you often publish source codes? Find out how to do that correctly!
Rodrigo Malacarne
Moderator
9889
Rodrigo Malacarne 2013.11.28 14:38 

Suppose I have an array called StringArray[ ] filled with five string values like shown below:

StringArray[5] = {"ABC100","XYZ100","ABC200","XYZ200","ABC300"}

I want to search this array for all string values starting with "ABC".

The desired answer would be the position of these elements in the array, i.e., positions {0, 2, 4}.

However, after reading the docs I only found the function ArrayBSearch( ), which can only be used to find numeric values in an array.

Is there any way in MQL5 to perform such task?

Thanks in advance! 

Stephen Njuki
5179
Stephen Njuki 2013.11.28 16:24  
Malacarne:

Suppose I have an array called StringArray[ ] filled with five string values like shown below:

I want to search this array for all string values starting with "ABC".

The desired answer would be the position of these elements in the array, i.e., positions {0, 2, 4}.

However, after reading the docs I only found the function ArrayBSearch( ), which can only be used to find numeric values in an array.

Is there any way in MQL5 to perform such task?

Thanks in advance! 

You'd have to use a for loop...
Alain Verleyen
Moderator
30707
Alain Verleyen 2013.11.28 16:26  

You can either use a loop for each element of your array, and use the function StringFind()

Or you can use the class CArrayString from the Standard Library, probably with on of the method SearchXXX.

Rodrigo Malacarne
Moderator
9889
Rodrigo Malacarne 2013.11.28 21:15  

Thanks for the comments...

The solution I've implemented is shown below:

   for(int i=0;i<ArraySize(StringArray);i++)
     {
      if(StringFind(StringArray[i],"ABC") != -1)
        {
         Print("Position "+i+" = "+StringArray[i]);
        }
     }
To add comments, please log in or register