C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Trim Funktion für Strings

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Michael Gebhart
Guest





PostPosted: Sat Oct 09, 2004 2:42 pm    Post subject: Trim Funktion für Strings Reply with quote



Hi zusammen,

gibt es bereits eine fertige Trim-Funktion für Strings? So dass ich
Leerzeichen am Anfang und Ende entfernen kann? Oder muss ich diese selbst
schreiben?

Gruß

Michael

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
King Leo - Martin Oberzal
Guest





PostPosted: Sun Oct 10, 2004 11:39 am    Post subject: Re: Trim Funktion für Strings Reply with quote



Michael Gebhart wrote:
Quote:
gibt es bereits eine fertige Trim-Funktion für Strings? So dass ich
Leerzeichen am Anfang und Ende entfernen kann? Oder muss ich diese selbst
schreiben?

http://cvs.sourceforge.net/viewcvs.py/xstow/xstow/src/string_utils.cpp?rev=1.7&view=markup

std::string strip( const std::string& str,
const std::string& what = " tnr" )
{
if( str.empty() )
return std::string();

std::string::size_type start = str.find_first_not_of( what );
std::string::size_type end = str.find_last_not_of( what );

if( start == std::string::npos )
start = 0;

if( !end == std::string::npos )
if( end - start + 1 <= 0 )
return std::string();

return str.substr( start, end - start + 1 );
}

Gruß, Martin!

--
Quote:
Echte Programmier schreiben copy con Programm.exe Ganz echte Programmier
schreiben copy con Programm.zip
oder sie schreiben "cat /dev/dsp >programm" und pfeifen ins mikrophon

Hartmut Ott und Till Crueger in de.talk.jokes

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Hubert Schmid
Guest





PostPosted: Sun Oct 10, 2004 12:13 pm    Post subject: Re: Trim Funktion für Strings Reply with quote



King Leo - Martin Oberzalek <kingleo (AT) gmx (DOT) at> writes:

Quote:
std::string strip( const std::string& str,
const std::string& what = " tnr" )
{
if( str.empty() )
return std::string();

std::string::size_type start = str.find_first_not_of( what );
std::string::size_type end = str.find_last_not_of( what );

if( start == std::string::npos )
start = 0;

if( !end == std::string::npos )
if( end - start + 1 <= 0 )
return std::string();

return str.substr( start, end - start + 1 );
}

Ich finde den Code unnötig kompliziert. Folgender Code unterscheidet
nur zwei Fälle: Entweder der String besteht nur aus Whitespace oder
nicht.

std::string strip(
const std::string& str,
const std::string& what = " tnr")
{
std::string::size_type p = str.find_first_not_of(what);
if (p == std::string::npos) {
return std::string();
} else {
std::string::size_type q = str.find_last_not_of(what);
return std::string(str, p, q - p + 1);
}
}

Gruß, Hubert

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Stefan Reuther
Guest





PostPosted: Sun Oct 10, 2004 1:04 pm    Post subject: Re: Trim Funktion für Strings Reply with quote

Michael Gebhart wrote:
Quote:
gibt es bereits eine fertige Trim-Funktion für Strings?

Nein.

Quote:
So dass ich Leerzeichen am Anfang und Ende entfernen kann?
Oder muss ich diese selbst schreiben?

Der String-Bibliothek fehlen einige nützliche Dinge (z.B. auch ein
toupper). Das liegt wohl daran, dass es nicht ganz einfach ist, eine
Version zu definieren, die in allen Anwendungsfällen passt. Martin hat
seine Version gepostet. Hier ist meine:
using std::isspace;
typedef std::string string_t;
// ...

/** Trim leading (left) whitespace. Returns string s, with leading
whitespace (see #isspace) removed. */
string_t
strLTrim(string_t s)
{
string_t::size_type n = 0;
while (n < s.length() && isspace(s[n]))
++n;
return s.substr(n);
}

/** Trim trailing (right) whitespace. Returns string s, with trailing
whitespace (see #isspace) removed. */
string_t
strRTrim(string_t s)
{
string_t::size_type n = s.length();
while (n > 0 && isspace(s[n-1]))
--n;
return s.substr(0, n);
}
Damit hätten wir schon zwei sinnvolle Implementationen :)


Stefan

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Andreas Huennebeck
Guest





PostPosted: Mon Oct 11, 2004 8:59 am    Post subject: Re: Trim Funktion für Strings Reply with quote

Hubert Schmid wrote:

Quote:
Ich finde den Code unnötig kompliziert. Folgender Code unterscheidet
nur zwei Fälle: Entweder der String besteht nur aus Whitespace oder
nicht.

std::string strip(
const std::string& str,
const std::string& what = " tnr")
{
std::string::size_type p = str.find_first_not_of(what);
if (p == std::string::npos) {
return std::string();

D.h., wenn am Anfang kein Whitespace ist, wird auch am Ende nichts
abgeschnitten.

Quote:
} else {
std::string::size_type q = str.find_last_not_of(what);
return std::string(str, p, q - p + 1);
}
}

Tschau
Andreas
--
Andreas Hünnebeck | email: [email]ah (AT) despammed (DOT) com[/email]
----- privat ---- | www : http://www.huennebeck-online.de
Fax/Anrufbeantworter: 0721/151-284301
GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Andreas Huennebeck
Guest





PostPosted: Mon Oct 11, 2004 8:59 am    Post subject: Re: Trim Funktion für Strings Reply with quote

Michael Gebhart wrote:

Quote:
Hi zusammen,

gibt es bereits eine fertige Trim-Funktion für Strings? So dass ich
Leerzeichen am Anfang und Ende entfernen kann?

Nein.

Quote:
Oder muss ich diese selbst schreiben?

Nur abschreiben;-)

void StringStripSurround(string& str, const string& characters = " tnr")
{
string::size_type pos = str.find_first_not_of(characters);
if (string::npos != pos)
str = str.substr(pos);

pos = str.find_last_not_of(characters);
if (string::npos != pos)
str = str.substr(0, pos+1);
else
{
// it is still possible that 'str' contains only 'characters':
if (string::npos != str.find_first_of(characters))
str.erase();
}
}

Tschau
Andreas
--
Andreas Hünnebeck | email: [email]ah (AT) despammed (DOT) com[/email]
----- privat ---- | www : http://www.huennebeck-online.de
Fax/Anrufbeantworter: 0721/151-284301
GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Andreas Dunke
Guest





PostPosted: Mon Oct 11, 2004 9:56 am    Post subject: Re: Trim Funktion für Strings Reply with quote

Michael Gebhart wrote:

Quote:
gibt es bereits eine fertige Trim-Funktion für Strings? So dass ich
Leerzeichen am Anfang und Ende entfernen kann? Oder muss ich diese selbst
schreiben?

Ich hab da auch noch einen. Hab ich mal irgentwo im Netz gefunden, also
nicht von mir.

std::string right_trim( const std::string& s )
{
return s.substr(0,
std::distance(std::find_if(s.rbegin(),s.rend(),
std::not1(std::ptr_fun(isspace))),
s.rend()));
}


std::string left_trim( const std::string& s )
{
return s.substr(std::distance(s.begin(),
std::find_if(s.begin(),
s.end(),
std::not1(std::ptr_fun(isspace)))));
}


std::string trim( const std::string& s )
{
//We could just call left_trim(right_trim(s)), but that would
//involve unnecessary string temporaries. This should be a wee bit
//faster. (But maybe it's not worth it, or maybe the compiler would
//optimize out the temporaries -- it was fun writing it, though).

std::string::size_type beg = std::distance(s.begin(),
std::find_if(s.begin(),s.end(),
std::not1(std::ptr_fun(isspace))));

std::string::size_type end = std::distance(
std::find_if(s.rbegin(),s.rend(),
std::not1(std::ptr_fun(isspace))),
s.rend());

return s.substr(beg, end-beg);
}


Andreas

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Hubert Schmid
Guest





PostPosted: Mon Oct 11, 2004 5:21 pm    Post subject: Re: Trim Funktion für Strings Reply with quote

Andreas Huennebeck <ah (AT) despammed (DOT) com> writes:

Quote:
Hubert Schmid wrote:

std::string strip(
const std::string& str,
const std::string& what = " tnr")
{
std::string::size_type p = str.find_first_not_of(what);
if (p == std::string::npos) {
return std::string();

D.h., wenn am Anfang kein Whitespace ist, wird auch am Ende nichts
abgeschnitten.

Nein. Das heißt, wenn str *nur* Whitespace enthaelt, dann wird *alles*
abgeschnitten.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Andreas Huennebeck
Guest





PostPosted: Tue Oct 12, 2004 7:24 am    Post subject: Re: Trim Funktion für Strings Reply with quote

Hubert Schmid wrote:

Quote:
Andreas Huennebeck <ah (AT) despammed (DOT) com> writes:

Hubert Schmid wrote:

std::string strip(
const std::string& str,
const std::string& what = " tnr")
{
std::string::size_type p = str.find_first_not_of(what);
if (p == std::string::npos) {
return std::string();

D.h., wenn am Anfang kein Whitespace ist, wird auch am Ende nichts
abgeschnitten.

Nein. Das heißt, wenn str *nur* Whitespace enthaelt, dann wird *alles*
abgeschnitten.

Ja, natuerlich. Lesen muesste man koennen :-(

Tschau
Andreas
--
Andreas Hünnebeck | email: [email]ah (AT) despammed (DOT) com[/email]
----- privat ---- | www : http://www.huennebeck-online.de
Fax/Anrufbeantworter: 0721/151-284301
GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Frank Birbacher
Guest





PostPosted: Fri Oct 15, 2004 9:37 am    Post subject: Re: Trim Funktion für Strings Reply with quote

Hi!

Andreas Dunke wrote:
Quote:
std::string::size_type beg = std::distance(s.begin(),
std::find_if(s.begin(),s.end(),
std::not1(std::ptr_fun(isspace))));

std::string::size_type end = std::distance(
std::find_if(s.rbegin(),s.rend(),
std::not1(std::ptr_fun(isspace))),
s.rend());

return s.substr(beg, end-beg);
}

Ist substr möglicherweise schneller als "range construction" mit
string(Iter, Iter)?

Frank

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Oct 18, 2004 1:01 pm    Post subject: Re: Trim Funktion für Strings Reply with quote

Frank Birbacher <bloodymir.crap (AT) gmx (DOT) net> wrote


Quote:
Andreas Dunke wrote:
std::string::size_type beg = std::distance(s.begin(),
std::find_if(s.begin(),s.end(),
std::not1(std::ptr_fun(isspace))));

std::string::size_type end = std::distance(
std::find_if(s.rbegin(),s.rend(),
std::not1(std::ptr_fun(isspace))),
s.rend());

return s.substr(beg, end-beg);
}

Ist substr möglicherweise schneller als "range construction" mit
string(Iter, Iter)?

Möglicherweise. Eine Implementierung könnte z.B. COW auf der
ursprunglichen Kette benutzen, und aber getrennte Variablen für die
Länge und den Anfangsoffset. In solchem Fall könnte substr (und der
Constructor string( string, size_t, size_t ) den Copie vermeiden.
(Obwohl... durch Template-Metaprogrammierung könnte der Compiler
erkennen, wann Iter string::const_iterator war, und auch diese
Optimierung benutzen.)

In der Praxis habe ich den Fall nicht gesehen.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.