[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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
4
Medium Priority
?
326 Views
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?  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;
      }
0
Comment
Question by:lapucca
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16574848
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
 

Author Comment

by:lapucca
ID: 16574873
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 16574890
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
 

Author Comment

by:lapucca
ID: 16574921
Thank you very much for the correction.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

834 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