0

I am using C++ Builder 10.2 Tokyo, and I have to find the position of the last space in a String variable which has a value like "XXX YYYYY TYUR KXYZ";

If I use the function LastDelimiter(const System::UnicodeString Delimiters, const System::UnicodeString S) it returns 0, but if I use the UnicodeString method::LastDelimiter( UnicodeString Delimiter ) method it returns the length of the variable.

Here is the code I am using:

void TMyClass::SetVerbo()
{
    int nPosCut = 0;

    if( !this->Message.IsEmpty() ) // Message is a UnicodeString (String)
    {
        nPosCut = this->Message.LastDelimiter(String(" ")) + 1 ;
    }
    if( nPosCut > 0 )
        this->Verbo = this->Message.SubString(nPosCut, this->Message.Length() -nPosCut ) ;
}

While debugging the method call it gives me the correct value (34) although the result at nPosCut is 42 (Message length), as written in the code, or 0 if I change String(" ") to L" ".

What am I doing wrong?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Could you show how exactly you use these functions? A very simple [MCVE] would be of great value. Just edit your question and add the code to it. – Rudy Velthuis Dec 18 '18 at 23:14
  • 2
    Why are you using `AnsiString` at all, and not `UnicodeString`? `String` is an alias for `UnicodeString`. Also, the `AnsiString::LastDelimiter()` method simply calls the `Ansistrings::LastDelimiter()` function, so they can't have different return values for the same input. And there is no way the input string you have shown can return the string's length as its output, that is not where the last space character is located. I *suspect* you are dealing with a Unicode<->Ansi conversion that is throwing off the results. So yes, as Rudy said, please provide a [mcve] demonstrating the problem. – Remy Lebeau Dec 19 '18 at 00:04
  • I have added the code I am using. Thanks a lot. – Jayme Jeffman Dec 19 '18 at 12:51
  • As a matter of fact the function SetVerbo() was defined in the Header file. I have just move it to Code (cpp) file and it is working now. I would like to understand why this situation happens, code in the header file not working, it is not the first time I have got this issue. Thank you very much for your assistence. – Jayme Jeffman Dec 19 '18 at 13:07
  • 1
    @Jayme: hard to tell without knowing how your full header and cpp files look, but generally, it is never a good idea to put code that is more than trivial in a header. – Rudy Velthuis Dec 19 '18 at 15:47
  • 1
    There is little difference in putting a method's implementation in the `.h` file vs the `.cpp` file (except that code in the `.h` file is a candidate for inlining at call sites). So something else has to be going on that we can't see. Also, the code you have shown can be simplified to this: `void TMyClass::SetVerbo() { int nPosCut = Message.LastDelimiter(_D(" ")); if( nPosCut > 0 ) Verbo = Message.SubString(nPosCut + 1, MaxInt); }` Or this: `void TMyClass::SetVerbo() { int nPosCut = Message.LastDelimiter(_D(" ")); if( nPosCut > 0 ) Verbo = RightStr(Message, Message.Length() - nPosCut); }` – Remy Lebeau Dec 19 '18 at 21:38
  • 1
    @Can't check that right now, but what if #include is #included in the .cpp file *after* the .h file? Couldn't that cause the problems described? – Rudy Velthuis Dec 20 '18 at 09:21
  • 1
    @JaymeJeffman Could you check unitarily that `LastDelimiter` is OK inside the function? For example, add the following code and check variable value in the debugger `UnicodeString s = L"XXX YYYYY TYUR KXYZ"; int nPosCut = s.LastDelimiter(L" ");` – serge Dec 20 '18 at 10:04
  • I have made a test application having just a class with two UnicodeString variables and three methods, one for clear variable values and two for setting the String value to one of the variables. One method defined in the .h file and the other, with exactly the same code in the cpp file. Both methods worked fine. It might be another condition wich is causing the problem. – Jayme Jeffman Dec 20 '18 at 20:44

0 Answers0