This is my third article on getting Started
background
Getting the content between the delimiters is to get the content of the string at the specified position by dividing the incoming string according to the delimiter.
To follow the rules
We first determine the location of the string to extract and loop through to get the string before the specified delimiter.
Each character is compared to the specified delimiter. If the comparison is consistent, the character is considered to be preceded by the delimiter.
For Chinese character segmentation, check whether the current character to be separated is a Chinese character. If it is a Chinese character, check whether the character +1 is the delimiter.
When the loop reaches the position of the string to be extracted, the retrieved string is copied to the outgoing string, and the outgoing string is assigned a value of 0 if the loop has been completed to split the field position less than the position of the string to be extracted.
The implementation code
int isChina(int n, char *s)
{
int f = 0, i;
int Eend;
unsigned char *Buf;
Buf = (unsigned char *)s;
Eend = strlen((char *)Buf);
if (n < Eend)
{
for (i = 0; i <= n; ++i)
switch (f)
{
case 0:
if (Buf[i] > 128)
f = 1;
break;
case 1:
f = 0;
break;
};
}
if ((n == (Eend - 1)) && (f == 1))
f = 0;
return (f);
}
int prv_getSubStr(char *psString, long lNo, char *p3, char cFlag)
{
long lCnt;
char *p1, *p2;
char sOnHanZi[3];
lNo = lNo - 1;
p2 = psString;
for (lCnt = 0; lCnt <= lNo; lCnt++)
{
p1 = p2;
while(*p2 ! ='\ 0')
{
memset(sOnHanZi, 0x00.sizeof(sOnHanZi));
memcpy(sOnHanZi, p2, sizeof(char) * 2);
if (isChina(0, sOnHanZi) ! =0)
{
p2++;
p2++;
continue;
}
if (*p2 == cFlag)
{
break;
}
p2++;
}
if (*p2 == '\ 0' || lCnt == lNo)
break;
p2++;
}
if (lCnt < lNo)
{
p3[0] = 0;
return 0;
}
memcpy(p3, p1, p2 - p1);
p3[p2 - p1] = '\ 0';
return 0;
}
Copy the code
The functionality
- The function name: prv_getSubStr
- Gets the contents of the specified position between the delimiters
- PsString: source string
- LNo: indicates the position number
- P3: indicates the destination string
- CFlag: separator
- Return code — 0: Success