std::string question
-
Consider declaration,
std::string name = " ABCD"
As can be seen above the string consist of many "space bar" characters in the beginning. I want to skip them and get the resultat string value. Please tell me how ????name.substr(name.find_first_not_of(' '));
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Consider declaration,
std::string name = " ABCD"
As can be seen above the string consist of many "space bar" characters in the beginning. I want to skip them and get the resultat string value. Please tell me how ????This would be easiest:
std::string name = "ABCD";
Now seriously: you should use string::find_first_not_of:
int pos = name.find_first_not_of(' ');
if (pos != std::string::npos)
{
name.erase(0, pos);
}If you want to remove all spaces (not only at beginning), use this:
name.erase(std::remove(name.begin(), name.end(), ' '), name.end());
Tomasz Sowinski -- http://www.shooltz.com
** Putt knot yore thrust inn spel chequers. **
-
Consider declaration,
std::string name = " ABCD"
As can be seen above the string consist of many "space bar" characters in the beginning. I want to skip them and get the resultat string value. Please tell me how ????Should you use
CString
you have the following methods:TrimLeft
,TrimRight
. Best regards, Alexandru Savescu -
Should you use
CString
you have the following methods:TrimLeft
,TrimRight
. Best regards, Alexandru Savescu(with tremulous voice) Don't lead the guy apart from the one and true
std::string
! Now seriously, these functions, though not directly available, can be easily written:/* String trimming templates.
* Written by Markus B. Krüger (markusk@pvv.org)
* Found at USENET:
* Subject: Re: trim in standard lib?
* Newsgroups: comp.lang.c++.moderated
* Date: 2001-07-13 19:21:08 PST
* URL (Google): http://groups.google.com/groups?hl=en&selm=du38zhtjjze.fsf%40proto.pvv.ntnu.no
*
* NB: There's a typo on the original message. On trim_right(String& str, Ch ws), replace
*
* if ((last_nonblank = str.find_last_not_of(' ')) != String::npos)
*
* with
* if ((last_nonblank = str.find_last_not_of(ws)) != String::npos)
*/
#ifndef TRIMTEMPLATES_H
#define TRIMTEMPLATES_H
#define VERSION_TRIMTEMPLATES 0x00010000
template<class String,class Ch>
inline String& trim_left(String& str,Ch ws)
{
typename String::size_type first_nonblank;
if ((first_nonblank=str.find_first_not_of(ws))!=String::npos)
return str.erase(0, first_nonblank);
else
return str;
}
template<class String>
inline String& trim_left(String& str)
{
return trim_left(str, ' ');
}
template<class String,class Ch>
inline String& trim_right(String& str,Ch ws)
{
typename String::size_type last_nonblank;
if((last_nonblank=str.find_last_not_of(ws))!= String::npos)
return str.erase(last_nonblank+1);
else
return str;
}
template<class String>
inline String& trim_right(String& str)
{
return trim_right(str, ' ');
}
template<class String,class Ch>
inline String& trim(String& str,Ch ws)
{
return trim_left(trim_right(str, ws),ws);
}
template<class String>
inline String& trim(String& str)
{
return trim_left(trim_right(str));
}
#elif VERSION_TRIMTEMPLATES!=0x00010000
#error You have included two LISTPORTS.H with different version numbers
#endifJoaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
(with tremulous voice) Don't lead the guy apart from the one and true
std::string
! Now seriously, these functions, though not directly available, can be easily written:/* String trimming templates.
* Written by Markus B. Krüger (markusk@pvv.org)
* Found at USENET:
* Subject: Re: trim in standard lib?
* Newsgroups: comp.lang.c++.moderated
* Date: 2001-07-13 19:21:08 PST
* URL (Google): http://groups.google.com/groups?hl=en&selm=du38zhtjjze.fsf%40proto.pvv.ntnu.no
*
* NB: There's a typo on the original message. On trim_right(String& str, Ch ws), replace
*
* if ((last_nonblank = str.find_last_not_of(' ')) != String::npos)
*
* with
* if ((last_nonblank = str.find_last_not_of(ws)) != String::npos)
*/
#ifndef TRIMTEMPLATES_H
#define TRIMTEMPLATES_H
#define VERSION_TRIMTEMPLATES 0x00010000
template<class String,class Ch>
inline String& trim_left(String& str,Ch ws)
{
typename String::size_type first_nonblank;
if ((first_nonblank=str.find_first_not_of(ws))!=String::npos)
return str.erase(0, first_nonblank);
else
return str;
}
template<class String>
inline String& trim_left(String& str)
{
return trim_left(str, ' ');
}
template<class String,class Ch>
inline String& trim_right(String& str,Ch ws)
{
typename String::size_type last_nonblank;
if((last_nonblank=str.find_last_not_of(ws))!= String::npos)
return str.erase(last_nonblank+1);
else
return str;
}
template<class String>
inline String& trim_right(String& str)
{
return trim_right(str, ' ');
}
template<class String,class Ch>
inline String& trim(String& str,Ch ws)
{
return trim_left(trim_right(str, ws),ws);
}
template<class String>
inline String& trim(String& str)
{
return trim_left(trim_right(str));
}
#elif VERSION_TRIMTEMPLATES!=0x00010000
#error You have included two LISTPORTS.H with different version numbers
#endifJoaquín M López Muñoz Telefónica, Investigación y Desarrollo
Joaquín M López Muñoz wrote: Don't lead the guy apart from the one and true std::string! I never meant that! I like STL just as much as you do. I was just showing some MFC code ;) Best regards, Alexandru Savescu
-
Joaquín M López Muñoz wrote: Don't lead the guy apart from the one and true std::string! I never meant that! I like STL just as much as you do. I was just showing some MFC code ;) Best regards, Alexandru Savescu
SHAME! SHAME! You should know better than to show MFC examples. People will get the mistaken impression its easier to use than the beloved STL. :-D
-
SHAME! SHAME! You should know better than to show MFC examples. People will get the mistaken impression its easier to use than the beloved STL. :-D
Take it easy man! I was not giving any MFC example, I was giving a CString example. CString is a very good class it can be argued that it is better than std::string (it uses reference counting remember!) Best regards, Alexandru Savescu
-
Take it easy man! I was not giving any MFC example, I was giving a CString example. CString is a very good class it can be argued that it is better than std::string (it uses reference counting remember!) Best regards, Alexandru Savescu
Alexpro wrote: it is better than std::string SACRILEDGE!! Alexpro wrote: CString is a very good class BLASPHEMY!! Oh NO! STr o k e... BTW CString is part of MFC. (Microsoft Foul Code). Everyone knows was developed as part of a feindish plot to undermine portablilty with Unix/Linux.:rolleyes: