Is there builtin function in wstring that I can use to strip out leading and trailing spaces when I save the data from Editbox control?

Hi, Are there any function either in wstring or other fucntinos that I can use to strip out the leading and trailing spaces in the following code?  newUserData.shell  is a wstring variable, which I want to make sure that it will not store any leading or trailing blanks.  Thanks.

      LPWSTR syData = new WCHAR[ONE_K];

      numCharCopy = GetDlgItemText(hwndDlg, IDC_SHELL, syData, ONE_K);
      if((numCharCopy <= 0) && (newUserData.symarkEnabled))
      {
            ::MessageBox(NULL, L"Shell specification is mandatory.", L"Unity - SaveSyUserData", MB_OK);
                  setRet = false;
                  goto CleanUp;
      }
      else if(numCharCopy > 0)
      {
            newUserData.shell = syData;
            noData = false;
      }
lapuccaAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Yes, and I was wrong about 'erase()', too, but

#include <iostream>
#include <string>

using namespace std;


void StripBlanks ( wstring& str) {

while(str[0] == L' ') str.erase((int)0,1); // leading blanks

while(str[str.length() - 1] == L' ') str.erase(str.length() - 1,1); // trailing blanks
}

void main () {

wstring test = L"    test    ";

wcout << L"Before: " << test << L" " << test.length() << endl;

StripBlanks(test);

wcout << L"After: " << test << L" " << test.length() << endl;
}

works.
0
 
jkrCommented:
There's no built-in function, but you could use

void StripBlanks ( wstring& str) {

while(str[0] == L' ') str.erase(0); // leading blanks

while(str[str.length()] == L' ') str.erase(str.length()); // trailing blanks

}
0
 
lapuccaAuthor Commented:
Thank you jkr.  But wouldn't it be
while(str[str.length()-1] == L' ') str.erase(str.length()-1); // trailing blanks

for the trailing blanks?
0
 
lapuccaAuthor Commented:
Thank you very much for the correction.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.