问题描述
我有一个Unicode字符串,我希望限制为30个字符.我从查询中填充字符串,所以我不知道开头的长度.我只想剪掉30岁以后的所有字符.我找到了 UnicodeString::Delete()
方法,但是我不知道如何使用它.
I have a Unicode string that I want to limit to 30 characters. I populate the string from a query, so I don't know the length to begin with. I want to simply snip off all of the characters past 30. I found the UnicodeString::Delete()
method, but I don't know how to use it.
我尝试这样做无济于事
mystring = <code here to populate the unicode string mystring>
Delete(mystring, 30, 100);
推荐答案
您实际上是在尝试致电 System::Delete()
,仅对Delphi不可用,对C ++不可用.在内部,UnicodeString::Delete()
使用this
作为要操作的字符串来调用System::Delete()
.
You are actually trying to call System::Delete()
, which is not available to C++, only to Delphi. Internally, UnicodeString::Delete()
calls System::Delete()
using this
as the string to manipulate.
UnicodeString::Delete()
是非静态类方法.您需要在字符串对象本身上调用它,而不是作为单独的函数调用它.另外,Delete()
是1索引而不是0索引:
UnicodeString::Delete()
is a non-static class method. You need to call it on the string object itself, not as a separate function. Also, Delete()
is 1-indexed, not 0-indexed:
mystring.Delete(31, MaxInt);
如果要使用0索引,请使用 UnicodeString::Delete0()
代替:
If you want to use 0-indexing, use UnicodeString::Delete0()
instead:
mystring.Delete0(30, MaxInt);
但是, UnicodeString::SetLength()
方法将是在这种情况下更合适:
However, the UnicodeString::SetLength()
method would be more appropriate in this situation:
if (mystring.Length() > 30)
mystring.SetLength(30);
或者,您可以使用 UnicodeString::SubString()
/ UnicodeString::SubString0()
:
Alternatively, you can use UnicodeString::SubString()
/UnicodeString::SubString0()
:
mystring = mystring.SubString(1, 30);
mystring = mystring.SubString0(0, 30);
这篇关于UnicodeString :: Delete方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!