09.10.2016, 15:47
Fixed split function:
In contrast to the original function (which had an issue with out of bounds of the array "destination", if the string "source" had delimiters more than an array size of "destination"), this function requires to specify two-dimensional array size of "destination". So it have two new arguments: destsize (the size of the first dimension), destlen (and second).
Example:
Simple strfindchar functon:
This is faster than using strfind function, but only in case to search for a single character in the string.
It's useful only if you use this function in events that are often called (cycle, OnPlayerUpdate?) or for find a large number of single characters
Example:
In contrast to the original function (which had an issue with out of bounds of the array "destination", if the string "source" had delimiters more than an array size of "destination"), this function requires to specify two-dimensional array size of "destination". So it have two new arguments: destsize (the size of the first dimension), destlen (and second).
Code:
split(const source[], destination[][], delim, destsize, destlen) { for(new i, li, aNum, len, s = strlen(source); i <= s && aNum < destsize; i++) { if(source[i] == delim || i == s) { len = strmid(destination[aNum], source, li, i, destlen); destination[aNum][len] = EOS; li = i + 1; aNum++; } } return 1; }
Code:
new pos[3][9]; new strtmp[] = "1958.3783, 1343.1572, 15.3746 ||| Other s,t,u,f,f"; split(strtmp, pos, ',', sizeof pos, sizeof pos[]); SetPlayerPos(playerid, floatstr(pos[0]), floatstr(pos[1]), floatstr(pos[2]));
This is faster than using strfind function, but only in case to search for a single character in the string.
It's useful only if you use this function in events that are often called (cycle, OnPlayerUpdate?) or for find a large number of single characters
Code:
strfindchar(const string[], sub) { for(new i; string[i] != '\0'; i++) { if(string[i] == sub) return i; } return -1; }
Code:
new strtmp[] = "Question?"; if(strfindchar(strtmp, '?') != -1) //Pay attention to '' (not "") { print("Yes"); } else print("No");