trim for strings 
Author Message
 trim for strings

HI!
Are there a functions  TrimRight and TrimLeft for std::string (like in MFC
CString)?

Thank you.



Sun, 02 May 2004 03:16:15 GMT  
 trim for strings
No, but you can roll your own using find_first_not_of, find_last_not_of and
erase.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat, and
wrong." H.L. Mencken


Quote:
> HI!
> Are there a functions  TrimRight and TrimLeft for std::string (like in MFC
> CString)?

> Thank you.



Sun, 02 May 2004 03:41:12 GMT  
 trim for strings
This routine trims blanks right.

string rtrim (const string& str)
{
    size_type pos;
    pos = str.find_last_not_of (" "); //-- (You can
replace " " with " \r\n\f\v\t" if you desire to trim some
common control characters)
    return str.substr (0, pos + 1);

Quote:
}

This routine trims blanks left.

string ltrim (const string& str)
{
    size_type pos;
    pos = str.find_first_not_of (" "); //-- Idem
    if (pos != string::npos)
        return str.substr (pos);
    else
        return str;

Quote:
}
>-----Original Message-----
>No, but you can roll your own using find_first_not_of,

find_last_not_of and
Quote:
>erase.
>--
>With best wishes,
>    Igor Tandetnik

>"For every complex problem, there is a solution that is
simple, neat, and
>wrong." H.L. Mencken


message

>> HI!
>> Are there a functions  TrimRight and TrimLeft for

std::string (like in MFC
Quote:
>> CString)?

>> Thank you.

>.



Sun, 02 May 2004 04:50:12 GMT  
 trim for strings


Quote:
> This routine trims blanks right.

> string rtrim (const string& str)
> {
>     size_type pos;
>     pos = str.find_last_not_of (" "); //-- (You can
> replace " " with " \r\n\f\v\t" if you desire to trim some
> common control characters)
>     return str.substr (0, pos + 1);
> }

> This routine trims blanks left.

> string ltrim (const string& str)
> {
>     size_type pos;
>     pos = str.find_first_not_of (" "); //-- Idem
>     if (pos != string::npos)
>         return str.substr (pos);
>     else
>         return str;            <-- the BUG replace by return str.substr
(0, 0) ;
> }

I found a little bug in your ltrim routine with this little test
 string l_StrSource = "  abc  " ;
 string l_Str1 = rtrim (l_StrSource) ;
 string l_Str2 = ltrim (l_StrSource) ;

 l_StrSource = "  " ;
 l_Str1 = rtrim (l_StrSource) ;
 l_Str2 = ltrim (l_StrSource) ;            <--- BUG not empty

--
Serge Wenger



Sun, 02 May 2004 15:02:13 GMT  
 trim for strings
It's not in there you have to roll your own. Something like this maybe:

#include <string>
#include <algorithm>
#include <functional>
#include <ctype.h>

std::string trim_left(const std::string& str)
{
 std::string::const_iterator iter = std::find_if(str.begin(), str.end(),
std::not1(std::ptr_fun(isspace)));
 if (iter == str.end())
  iter = str.begin();
 return std::string(iter, str.end());

Quote:
}

std::string trim_right(const std::string& str)
{
 std::string::const_reverse_iterator iter = std::find_if(str.rbegin(),
str.rend(), std::not1(std::ptr_fun(isspace)));
 return std::string(str.begin(), iter.base());

Quote:
}

std::string trim(const std::string& str)
{
 std::string::const_iterator begin = std::find_if(str.begin(), str.end(),
std::not1(std::ptr_fun(isspace)));
 if (begin == str.end())
  begin = str.begin();
 std::string::const_reverse_iterator end = std::find_if(str.rbegin(),
str.rend(), std::not1(std::ptr_fun(isspace)));
 return std::string(begin, end.base());

Quote:
}

/Michel
www.exmentis.com

Quote:
> HI!
> Are there a functions  TrimRight and TrimLeft for std::string (like in MFC
> CString)?

> Thank you.



Tue, 04 May 2004 06:03:42 GMT  
 trim for strings

Quote:

> It's not in there you have to roll your own. Something like this maybe:

> #include <string>
> #include <algorithm>
> #include <functional>
> #include <ctype.h>

> std::string trim_left(const std::string& str)
> {
>  std::string::const_iterator iter = std::find_if(str.begin(), str.end(),
> std::not1(std::ptr_fun(isspace)));
>  if (iter == str.end())
>   iter = str.begin();
>  return std::string(iter, str.end());
> }

> std::string trim_right(const std::string& str)
> {
>  std::string::const_reverse_iterator iter = std::find_if(str.rbegin(),
> str.rend(), std::not1(std::ptr_fun(isspace)));
>  return std::string(str.begin(), iter.base());
> }

> std::string trim(const std::string& str)
> {
>  std::string::const_iterator begin = std::find_if(str.begin(), str.end(),
> std::not1(std::ptr_fun(isspace)));
>  if (begin == str.end())
>   begin = str.begin();
>  std::string::const_reverse_iterator end = std::find_if(str.rbegin(),
> str.rend(), std::not1(std::ptr_fun(isspace)));
>  return std::string(begin, end.base());
> }

> /Michel
> www.exmentis.com


> > HI!
> > Are there a functions  TrimRight and TrimLeft for std::string (like in MFC
> > CString)?

> > Thank you.

I notice no-one has suggested deriving a class from string and giving
it extra functionality. As this seems such an obvious thing to do (we
used to do it for CString before it contained Format()), I assume
there must be some argument against doing it. Is this the case, or am
I jumping to conclusions?

Pablo Canary



Tue, 04 May 2004 22:46:57 GMT  
 trim for strings

Quote:
> I notice no-one has suggested deriving a class from string and giving
> it extra functionality. As this seems such an obvious thing to do (we
> used to do it for CString before it contained Format()), I assume
> there must be some argument against doing it. Is this the case, or am
> I jumping to conclusions?

Frequently given answer is: std::string has a non-virtual destructor.
Publicly deriving from it may be dangerous because somebody migth
accidentally do something like this:

string *pStr = new DerivedFromString;
delete pStr;

which invokes undefined behavior. See

http://home.earthlink.net/~jmoleary/stdstring.htm

and pay attention to

http://home.earthlink.net/~jmoleary/stdstring_destructor.htm

--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat, and
wrong." H.L. Mencken



Tue, 04 May 2004 23:14:59 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. trim the string

2. trimming a string

3. How to trim a string in a _variant_t

4. How to trim a String?

5. Is there any easier way to trim the string?

6. Trim a comma off end of string???

7. Help to trim spaces of a string

8. String.Trim() behavior

9. String.Trim is this correct functionality

10. String.Trim();

11. Need help to trim spaces off a string

12. Right trim string function in C

 

 
Powered by phpBB® Forum Software