【发布时间】:2014-07-12 20:55:20
【问题描述】:
我有一个格式为联系人的字符串:
<abcde@123.343:5060;gr=xyz123>;+g134,<more_text>
我需要提取开<和闭>之间的内容
< 和 > 可以有多个实例,但我必须从存在参数 gr= 的一对中获取内容(仅出现一次)。
为了解决这个问题,我所做的是:
ptr = strstr(str,"gr=");
if(ptr)
{
temp1= ptr;
while(*temp1 && *temp1!='<')
{
temp1--;
}
strncpy(newstring,temp1,ptr-temp1); //will copy upto start of gr
temp2 = strstr(ptr,">")
if(temp2)
strncat(newstring,ptr,temp2-ptr); // copy remaining string till it finds closing '>'
}
它工作正常,但我想知道是否有避免while循环和倒退的方法?
【问题讨论】:
-
是的,还有其他方法可以做到这一点,但您为什么认为它们会更好?您可以做的一件事是一次性构建
newstring(从temp1到temp2)。