跳至内容

StringFind

StringFind

在字符串中搜索子字符串。

int  StringFind(
   string  string_value,        // 进行搜索的字符串
   string  match_substring,     // 搜索内容
   int     start_pos=0          // 搜索开始位置
   );

参量

string_value

[in] 字符串,在产生搜索中使用。

match_substring

[in] 寻找子串。

start_pos=0

[in] 从搜索开始位置索引。

返回值

如果未找到子字串符,从搜索子字串符开始返回字串符中的位置,或是 -1。

示例:

#define   RESERVE    100

//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 获取交易品种基础货币和利润货币
   string symbol_currency_base  =SymbolInfoString(Symbol(), SYMBOL_CURRENCY_BASE);
   string symbol_currency_profit=SymbolInfoString(Symbol(), SYMBOL_CURRENCY_PROFIT);
   PrintFormat("Symbol Currency Base: %s\nSymbol Currency Profit: %s", symbol_currency_base, symbol_currency_profit);

//--- 在循环中遍历服务器上所有可用的交易品种
   int total=SymbolsTotal(false), pos=-1;
   for(int i=0; i<total; i++)
     {
      //--- 获取下一个交易品种的名称
      string name=SymbolName(i, false);

      //--- 在交易品种名称中查找包含基础货币名称的子字符串,和
      //--- 如果找到子字符串,则在日志中显示交易品种名称、其在货币列表中的索引以及搜索到的货币名称
      pos = StringFind(name, symbol_currency_base);
      if(pos >= 0)
         PrintFormat("The '%s' symbol at index %u in the list contains the '%s' currency. Substring position in the symbol name: %d", name, i, symbol_currency_base, pos);

      //--- 在交易品种名称中查找包含报价货币名称的子字符串,和
      //--- 如果找到子字符串,则在日志中显示交易品种名称、其在货币列表中的索引以及搜索到的货币名称
      pos = StringFind(name, symbol_currency_profit);
      if(pos >= 0)
         PrintFormat("The '%s' symbol at index %u in the list contains the '%s' currency. Substring position in the symbol name: %d", name, i, symbol_currency_profit, pos);
     }

  /*
  结果
   StringFind (EURUSD,D1)   Symbol Currency Base: EUR
   StringFind (EURUSD,D1)   Symbol Currency Profit: USD
   The 'EURUSD' symbol at index 0 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURUSD' symbol at index 0 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'GBPUSD' symbol at index 1 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'USDCHF' symbol at index 2 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDJPY' symbol at index 3 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDCNH' symbol at index 4 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDRUB' symbol at index 5 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'AUDUSD' symbol at index 6 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'NZDUSD' symbol at index 7 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'USDCAD' symbol at index 8 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDSEK' symbol at index 9 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDHKD' symbol at index 10 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDSGD' symbol at index 11 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDNOK' symbol at index 12 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDDKK' symbol at index 13 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDTRY' symbol at index 14 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDZAR' symbol at index 15 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDCZK' symbol at index 16 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDHUF' symbol at index 17 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDPLN' symbol at index 18 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDRUR' symbol at index 19 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'EURAUD' symbol at index 27 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURCAD' symbol at index 28 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURCHF' symbol at index 29 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURCZK' symbol at index 30 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURDKK' symbol at index 31 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURGBP' symbol at index 32 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURHKD' symbol at index 33 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURHUF' symbol at index 34 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURJPY' symbol at index 35 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURNOK' symbol at index 36 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURNZD' symbol at index 37 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURPLN' symbol at index 38 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURRUR' symbol at index 39 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURRUB' symbol at index 40 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURSEK' symbol at index 41 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURTRY' symbol at index 42 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURZAR' symbol at index 43 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'XAUUSD' symbol at index 47 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'XAUEUR' symbol at index 48 in the list contains the 'EUR' currency. Substring position in the symbol name: 3
   The 'XAGUSD' symbol at index 50 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'XAGEUR' symbol at index 51 in the list contains the 'EUR' currency. Substring position in the symbol name: 3
   The 'USDCRE' symbol at index 53 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'XPDUSD' symbol at index 65 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'XPTUSD' symbol at index 66 in the list contains the 'USD' currency. Substring position in the symbol name: 3
   The 'USDGEL' symbol at index 67 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDMXN' symbol at index 68 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'EURMXN' symbol at index 69 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'USDCOP' symbol at index 75 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDARS' symbol at index 76 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDCLP' symbol at index 77 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'EURSGD' symbol at index 89 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'USDILS' symbol at index 95 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDTHB' symbol at index 122 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'USDRMB' symbol at index 123 in the list contains the 'USD' currency. Substring position in the symbol name: 0
   The 'EURILS' symbol at index 126 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'EURCNH' symbol at index 137 in the list contains the 'EUR' currency. Substring position in the symbol name: 0
   The 'USDBRL' symbol at index 139 in the list contains the 'USD' currency. Substring position in the symbol name: 0
  */
  }

另见

StringSubstrStringGetCharacterStringLenStringLen