gmclee@21cn.com wrote:
Hi there,
I need a program to extract a substring from the following pattern
xxxx<XXXXX>xxxx
therer, x and X are any character and the string between < and is
what I need. I use the following program to extract the substring, but
something wrong
char* deangle(const char* lpStr)
{
char *lpBuf = (char *)lpStr;
char *p = strchr( lpBuf, '<' ) + 1;
if (!p) return NULL;
char *q = strchr( lpBuf, '>' );
if (!q) return NULL;
*q = '\0'; /* to make sure no extra characters at the end of the
substring
memcpy( lpBuf, p, q-p );
return lpBuf;
}
apply the function to some string like xxxx<http://www.abc.com>xxxx
what I get is "http://www.abc.comm"
I am wondering where is the double m come?
Thanks in advance
I would do something like
void get_bracketed_string(const char s[], char result[])
{
char *langle, *rangle;
langle = strchr(s, '<');
rangle = strchr(s, '>');
if ((langle == NULL) || (rangle == NULL) || (langle rangle)) {
result = "";
} else {
strncpy(result, langle + 1, rangle - langle - 1);
result[rangle - langle - 1] = '\0';
}
}
August