StringSetLength
StringSetLength
设置一个字符串的指定长度(以字符为单位)。
bool StringSetLength(
string& string_var, // 字符串
uint new_length // 新字符串长度
);参数
- string_var
[in][out] 应该用于设置字符新长度的字符串。
- new_capacity
[in] 以字符为单位的所需字符串长度。如果new_length小于当前大小,则丢弃多余字符。
返回值
如果执行成功,返回true,否则 - false。若要接收错误代码,应该调用GetLastError()函数。
注意
StringSetLength()函数不会更改为字符串分配的缓冲区大小。
示例:
void OnStart()
{
//--- 定义字符串
string text="123456789012345";
//--- 在日志中显示字符串及其长度
PrintFormat("Before StringSetLength() the string '%s' has a size of %d characters", text, StringLen(text));
//--- 将字符串大小减少到10个字符
StringSetLength(text, 10);
//--- 将因StringSetLength()操作而变化的字符串及其新长度显示在日志中
PrintFormat("After StringSetLength() the string is now '%s', and has a size of %d characters", text, StringLen(text));
/*
结果
Before StringSetLength() the string '123456789012345' has a size of 15 characters
After StringSetLength() the string is now '1234567890', and has a size of 10 characters
*/
}另见
StringLen、StringBufferLen、 StringReserveStringInit、 StringSetCharacter