通过指定的分隔符从指定的字符串获取子字符串,返回获得的子字符串的数目。
int StringSplit(
const string string_value, //要搜索的字符串
const ushort separator, //分隔符,用于搜索子字符串
string & result[] // 数组,用来存储被分割的子字符串
);
分割出来的子字符串放在数组中。 如果在传递的字符串中没有找到分隔符,则只将一个源字符串放入数组中 。返回子字符串个数。
如果string_value为空或NULL,函数将返回0。 如果出现错误,函数返回-1 。
示例:
string to_split=”life_is_good”; // 要分割的字符串
string sep=”_”; // 分隔符
ushort u_sep; // 分隔符ASCII码
string result[]; // 存放子串的数组
//— 获取分隔符的字符值(ASCII码)
u_sep=StringGetCharacter(sep,0);
//— 分割子串
int k=StringSplit(to_split,u_sep,result);
//— 打印结果
PrintFormat(“Strings obtained: %d. Used separator ‘%s’ with the code %d”,k,sep,u_sep);
//— 输出所有子串
if(k>0)
{
for(int i=0;i<k;i++)
{
PrintFormat(“result[%d]=%s”,i,result[i]);
}
}