PHP Code:
public CenterString(const String:str[], String:output[], maxlength)
{
new length = strlen(str);
new size = maxlength;
new String:newstr[size];
new firstspace = RoundToCeil((size-length)/2.0);
new secondspace = RoundToFloor((size-length)/2.0);
for(new i=0; i < firstspace; i++)
{
newstr[i] = ' '; // change this if you want to center with non-whitespace
}
StrCat(newstr, size, str);
for(new j=length+firstspace; j < length+firstspace+secondspace; j++)
{
newstr[j] = ' '; // change this if you want to center with non-whitespace
}
strcopy(output, maxlength, newstr);
}
Useful if you're printing text and want it centered, also not a bad example of how to use a buffer to return strings.
Example:
PHP Code:
new String:test[32];
strcopy(test, sizeof(test), "Centering");
CenterString(test, test, sizeof(test));
returns: