08.11.2011, 21:46
strCopyEx - Copies a string to another string until it reaches the given delimiter:
Example(s):
Analog to this function:
strExplode - Splits a string in parts (like "explode" in PHP):
Example(s):
pawn Код:
stock strCopyEx(szSrc[], szDest[], const szDelim[], iSize = sizeof(szDest)) {
new
iPos
;
if((iPos = strfind(szSrc, szDelim[0], true)) != -1) {
szSrc[iPos] = EOS;
iSize = strcat(szDest, szSrc, iSize);
szSrc[iPos] = szDelim[0];
}
else {
strcat(szDest, szSrc, iSize);
}
return iSize;
}
pawn Код:
new
szStr[] = "This-is-a-test-string-!",
szDest[7]
;
strCopyEx(szStr[5], szDest, "-");
print(szDest); // Prints "is"
szDest[0] = EOS;
strCopyEx(szStr[15], szDest, "-");
print(szDest); // Prints "string"
strExplode - Splits a string in parts (like "explode" in PHP):
pawn Код:
stock strExplode(szStr[], const szDelim[], szOutput[][], const iSize = sizeof(szOutput)) {
new
i,
iPos,
iLen = strlen(szStr)
;
do {
iPos += strCopyEx(szStr[iPos], szOutput[i], szDelim[0], iLen) + 1;
}
while(iPos < iLen && ++i < iSize);
return i;
}
pawn Код:
new
szStr[] = "This-is-a-test-string-!",
szOutput[6][7] // We have 6 string parts and the biggest length is 7
;
strExplode(szStr, "-", szOutput);
print(szOutput[0]); // This
print(szOutput[1]); // is
print(szOutput[2]); // a
print(szOutput[3]); // test
print(szOutput[4]); // string
print(szOutput[5]); // !