01.03.2016, 03:51
strtrunc
Truncates a string. If the string is longer than the max chars specified, the excess characters are removed and replaced with "...".
Example:
Outputs:
Areas of use:
How you would normally do it without using the function:
How it looks like with this function:
Truncates a string. If the string is longer than the max chars specified, the excess characters are removed and replaced with "...".
Example:
pawn Код:
new
string[24];
strtrunc("This is a test string.", 10, string);
print(string);
pawn Код:
This is a ...
- Tab list dialogs where you don't want to show the entire string in the column (especially if it's long).
- Places where long strings can't normally fit and need to be truncated.
- And lots more!
pawn Код:
strtrunc(const source[], chars, dest[], maxlength=sizeof(dest))
{
if(strlen(source) > chars)
{
if(ispacked(source))
{
strmid(dest, source, 0, chars, maxlength);
strcat(dest, "...", maxlength);
}
else
{
format(dest, maxlength, "%.*s...", chars, source);
}
}
else
{
if(ispacked(source))
{
strpack(dest, source, maxlength);
}
else
{
strcat(((dest[0] = 0), dest), source, maxlength);
}
}
return 1;
}
pawn Код:
if(strlen(banReason) > 24)
{
format(string, sizeof(string), "%s\t%.24s...\t%s", username, banReason, date);
}
else
{
format(string, sizeof(string), "%s\t%s\t%s", username, banReason, date);
}
pawn Код:
new
truncatedReason[24];
strtrunc(banReason, 24, truncatedReason);
format(string, sizeof(string), "%s\t%s\t%s", username, truncatedReason, date);