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?

Posted on 2006-04-30
Last Modified: 2010-04-01
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?  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)
   = syData;
            noData = false;
Question by:lapucca
    LVL 86

    Expert Comment

    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


    Author Comment

    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?
    LVL 86

    Accepted Solution

    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;


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


    Author Comment

    Thank you very much for the correction.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now