Download MetaTrader 5

How to search an array with string values?

To add comments, please log in or register
Rodrigo Malacarne
Moderator
9636
Rodrigo Malacarne  

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
5241
Stephen Njuki  
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
29874
Alain Verleyen  

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
9636
Rodrigo Malacarne  

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