跳至内容

StringSplit

StringSplit

使用指定的分隔符从指定字符串中获取子字符串,并返回获得的子字符串数量。

int  StringSplit(
   const string   string_value,       // A string to search in
   const ushort   separator,          // A separator using which substrings will be searched
   string         & result[]          // An array passed by reference to get the found substrings
   );

参数

string_value

[输入] 需要从中获取子字符串的字符串。该字符串保持不变。

pos

[输入] 分隔符字符的位置。要获取位置,可以使用StringGetCharacter()函数。

result[]

[输出] 包含获得的子字符串的字符串数组。

返回值

result[]数组中子字符串的数量。如果分隔符未在传递的字符串中找到,则数组中只包含一个源字符串。

如果string_value为空或NULL,函数将返回零。发生错误时,函数返回-1。要获取错误代码,请调用GetLastError()函数。

示例:

string to_split="life_is_good";   // A string to split into substrings
   string sep="_";                // A separator as a character
   ushort u_sep;                  // The code of the separator character
   string result[];               // An array to get strings
   //--- Get the separator code
   u_sep=StringGetCharacter(sep,0);
   //--- Split the string to substrings
   int k=StringSplit(to_split,u_sep,result);
   //--- Show a comment
   PrintFormat("Strings obtained: %d. Used separator '%s' with the code %d",k,sep,u_sep);
   //--- Now output all obtained strings
   if(k>0)
     {
      for(int i=0;i<k;i++)
        {
         PrintFormat("result[%d]=%s",i,result[i]);
        }
     }

另请参阅

StringReplace(), StringSubstr(), StringConcatenate()

最后更新于