#if !defined isnull
#define isnull(%1) (((%1[0]) == '\0') || (((%1[0]) == '\1') && ((%1[1]) == '\0')))
#endif
/**
* Replace a substring in a string
* @param string The source string
* @param find The substring to be replaced
* @param replace The substring to replace with
* @param ignorecase If the search is case insensitive
* @param count The number of substrings to be replaced
* @param maxlength The length of the source string
* @return The number of substrings replaced
*/
stock strreplace(string[], const find[], const replace[], bool:ignorecase=false, count=cellmax, maxlength=sizeof(string))
{
if(isnull(find) || isnull(string)) return 0;
new matches;
for(new idx, flen = strlen(find), rlen = strlen(replace), pos = strfind(string, find, ignorecase); pos != -1 && idx < maxlength && matches < count; pos = strfind(string, find, ignorecase, idx)) {
strdel(string, pos, pos + flen);
if(rlen) strins(string, replace, pos, maxlength); // Crashy
idx = pos + rlen;
matches++;
}
return matches;
}
/**
* Replace a character in a string
* @param string The source string
* @param oldchar The character to be replaced
* @param newchar The character to replace with
* @return The number of characters replaced
*/
stock strreplacechar(string[], oldchar, newchar)
{
new matches;
if(ispacked(string)) {
if(newchar == '\0') {
for(new i; string{i} != '\0'; i++) {
if(string{i} == oldchar) {
strdel(string, i, i + 1);
matches++;
}
}
} else {
for(new i; string{i} != '\0'; i++) {
if(string{i} == oldchar) {
string{i} = newchar;
matches++;
}
}
}
} else {
if(newchar == '\0') {
for(new i; string[i] != '\0'; i++) {
if(string[i] == oldchar) {
strdel(string, i, i + 1);
matches++;
}
}
} else {
for(new i; string[i] != '\0'; i++) {
if(string[i] == oldchar) {
string[i] = newchar;
matches++;
}
}
}
}
return matches;
}