c++builder-10.2-tokyo

UnicodeString::Delete Method


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.

I tried this to no avail:

mystring = <code here to populate the unicode string mystring>
Delete(mystring, 30, 100);

Solution

  • 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() 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);
    

    If you want to use 0-indexing, use UnicodeString::Delete0() instead:

    mystring.Delete0(30, MaxInt);
    

    However, the UnicodeString::SetLength() method would be more appropriate in this situation:

    if (mystring.Length() > 30)
        mystring.SetLength(30);
    

    Alternatively, you can use UnicodeString::SubString()/UnicodeString::SubString0():

    mystring = mystring.SubString(1, 30);
    

    mystring = mystring.SubString0(0, 30);