Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't remember how to trim the last character typed using Trim function, I think using var.TrimRight(??????) is what I need. Can anyone refresh my memory?

Thanks,
-DrB
Posted
Updated 11-Feb-12 16:42pm
v2

Your solution 3 indicates that you are using a CString object. So you may:
C++
// remove last character
int nLen = tempDesc.GetLength();
if (nLen)
    tempDesc = tempDesc.Left(nLen - 1);

// trim to max. length
int nLen = tempDesc.GetLength();
if (nLen > nMaxLen)
    tempDesc = tempDesc.Left(nMaxLen);
 
Share this answer
 
(I don't have VS around here, so I cannot check the specifics)

2 things:

- If you want to limit the number of characters in your editbox, you can set it in the resource editor (I think) or via the DDX mechanism.

- If you do not want to limit the number of characters in the editbox, just handle the few messages and call CString::TrimRight after finding by index the character at the index 55.

M.
 
Share this answer
 
Comments
DrBones69 13-Feb-12 21:49pm    
Yes, with DDV mechanism you can limit the number of characters "somewhat", it just displays a message to the user that they have typed passed the max num ch. But, if you click OK you can add another ch and message appears again...as many times as your heart desires.
DrBones69 13-Feb-12 21:53pm    
I like your second suggestion, but I'm not sure I know how. I'm storing the user data in a CString variable. Normally, I would use a "for each" statement and this approach would work great. for each is not allowed with a CString. Any suggestion?
First of all, it's the best to use a memory refresher: [^].

However, what you need is not exactly a trim (the method "Trim" is Delphi and .NET is used to remove leading and/or trailing characters from some set of characters, blank character by default; this is not what you want).

If this is a null-terminated strings, you can just write a null character at the position of a last character.

With std::string, simply use resize to reduce the current size by one, see http://www.cplusplus.com/reference/string/string/resize/[^].

—SA
 
Share this answer
 
v2
Comments
DrBones69 11-Feb-12 22:19pm    
I'm using an event handler OnEnChange to monitor the length of what the user is typing into the edit box, when they reach >55 characters I display a message and what I would like to do is erase the last character after they click OK. I know this can be done with TrimRight, but I can't remember how.
Sergey Alexandrovich Kryukov 11-Feb-12 22:41pm    
System.String.Trim (TrimLeft, TrimRight) is from .NET, C++/CLI, not C++. You tag MFC, which is not .NET; it's C++. For this case, I've given you all you need. You don't need Trim to erase your last character.
--SA
Sergey Alexandrovich Kryukov 11-Feb-12 22:42pm    
What's the problem? What do you mean "how"? Find you TrimRight from Google. But I already answered -- you don't need it.
--SA
DrBones69 11-Feb-12 22:44pm    
Sorry, I meant MFC. Can you specify the 56th character (whatever it might be) the one to be removed. Or should I use something else?
C++
if(tempDesc.GetLength() > 60)
    {
        AfxMessageBox(L"Description can not be greater than 60 characters, any further characters typed will be ignored.");
        tempDesc.SetAt(tempDesc.GetLength() - 1, L'');    // Set last character typed as a space
        tempDesc.TrimRight();    // If you don't use the trim function you get caught in an infinite loop
        m_wndDesc1.SetWindowTextW(tempDesc);    // Display what has changed
        return;
    }


This is what I came up with, if anyone can predict a future problem with this, please let me know.

Thanks,
-DrB
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900