<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[leerzeichen entfernen]]></title><description><![CDATA[<p>kann ich mit dem STL string die leeren Stellen vor und hinter einem Text entfernen lassen oder muss ich mir selber was coden?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/163561/leerzeichen-entfernen</link><generator>RSS for Node</generator><lastBuildDate>Sun, 13 Sep 2026 12:28:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/163561.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 Oct 2006 21:01:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to leerzeichen entfernen on Mon, 30 Oct 2006 21:01:56 GMT]]></title><description><![CDATA[<p>kann ich mit dem STL string die leeren Stellen vor und hinter einem Text entfernen lassen oder muss ich mir selber was coden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1164928</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1164928</guid><dc:creator><![CDATA[franz k.]]></dc:creator><pubDate>Mon, 30 Oct 2006 21:01:56 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Mon, 30 Oct 2006 21:12:41 GMT]]></title><description><![CDATA[<p>Hallo Franz,</p>
<p>entweder selber coden</p>
<pre><code class="language-cpp">#include &lt;cctype&gt;
#include &lt;string&gt;

std::string trimm( std::string txt )
{
    using namespace std;
    while( !txt.empty() &amp;&amp; isspace( *txt.begin() ) )
        txt.erase( txt.begin() );
    while( !txt.empty() &amp;&amp; isspace( *(txt.end()-1) ) )
        txt.erase( txt.end()-1 );
    return txt;
}
</code></pre>
<p>oder besser: gar nicht erst dazu kommen lassen. Wo kommt der String her?</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1164934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1164934</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 30 Oct 2006 21:12:41 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 09:13:32 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/7393">@Werner</a>: Das geht aber noch deutlich schneller, indem du per find_if() das erste/letzte Nicht-Whitespace suchst und dann den Bereich dazwischen per substr() ausschneidest.<br />
(gerade Löschen am Stringanfang ist eher langsam)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1165973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1165973</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 01 Nov 2006 09:13:32 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 09:16:52 GMT]]></title><description><![CDATA[<p>sicher? speicher allozieren kostet bestimmt mehr als die array elemente zu verschieben. aber vielleicht sollte man es so proggen das nur 1 mal erase für jede seite aufgerufen wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1165976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1165976</guid><dc:creator><![CDATA[cul]]></dc:creator><pubDate>Wed, 01 Nov 2006 09:16:52 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 09:20:22 GMT]]></title><description><![CDATA[<p>achso er legt ja eh eine kopie an</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1165982</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1165982</guid><dc:creator><![CDATA[cul]]></dc:creator><pubDate>Wed, 01 Nov 2006 09:20:22 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 09:30:47 GMT]]></title><description><![CDATA[<p>Bei ganz langen Strings könnte es sich lohnen, anstatt find_if, find_first_not_of und find_last_not_of zu benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1165990</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1165990</guid><dc:creator><![CDATA[Konstruktör]]></dc:creator><pubDate>Wed, 01 Nov 2006 09:30:47 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 09:34:41 GMT]]></title><description><![CDATA[<p>cul schrieb:</p>
<blockquote>
<p>sicher? speicher allozieren kostet bestimmt mehr als die array elemente zu verschieben.</p>
</blockquote>
<p>Ja, sicher. Schließlich verschiebt jedes erase() die Elemente wieder neu (und dieses mehrfache Verschieben kostet).</p>
<blockquote>
<p>aber vielleicht sollte man es so proggen das nur 1 mal erase für jede seite aufgerufen wird.</p>
</blockquote>
<p>Oder so.</p>
<p>Edit:</p>
<p>Konstruktör schrieb:</p>
<blockquote>
<p>Bei ganz langen Strings könnte es sich lohnen, anstatt find_if, find_first_not_of und find_last_not_of zu benutzen.</p>
</blockquote>
<p>Durchaus möglich - aber dazu müsstest du erstmal einen Teststring zusammenbauen mit allen Whitespaces (in der Hinsicht sind die Algorithmen vielseitiger).</p>
<p>(btw kann man find_if auch mit reverse-Iteratoren verwenden ;))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1165993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1165993</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 01 Nov 2006 09:34:41 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 09:59:19 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>Konstruktör schrieb:</p>
<blockquote>
<p>Bei ganz langen Strings könnte es sich lohnen, anstatt find_if, find_first_not_of und find_last_not_of zu benutzen.</p>
</blockquote>
<p>Durchaus möglich - aber dazu müsstest du erstmal einen Teststring zusammenbauen mit allen Whitespaces</p>
</blockquote>
<p>Wieso das? 'find_first/last_not_of' funktioniert wunderbar.</p>
<pre><code class="language-cpp">std::string trim(std::string str)
{
    std::string::size_type i;

    if ((i = str.find_first_not_of(&quot; \t&quot;)) != std::string::npos)
        str.erase(0, i);
    else return &quot;&quot;;

    if ((i = str.find_last_not_of(&quot; \t&quot;)) != std::string::npos)
        str.erase(i + 1);

    return str;
}
</code></pre>
<p>/EDIT: Das 'else' sollte man sich eher sparen. Das macht das ganze nicht wirklich schneller und verhindert RVO.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166012</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166012</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Wed, 01 Nov 2006 09:59:19 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 10:01:04 GMT]]></title><description><![CDATA[<p>Und was ist mit \n etc?</p>
<p>MSDN schrieb:</p>
<blockquote>
<p>isspace returns a non-zero value if c is a white-space character (0x09 – 0x0D or 0x20).</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1166018</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166018</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 01 Nov 2006 10:01:04 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 10:15:51 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>Und was ist mit \n etc?</p>
</blockquote>
<p>Hm. Ich finde eigentlich nicht, dass diese durch 'trim' entfernt werden sollen aber ich bin da zugegebenermaßen durch VB vorbelastet.<br />
Wenn man äquivalent zu 'isspace' agieren möchte sollte man das auch benutzen, sprich über 'find_if' gehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166032</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166032</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Wed, 01 Nov 2006 10:15:51 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 10:42:33 GMT]]></title><description><![CDATA[<p>Ok, ich habe nen Gehirnstau. Kann man es weniger umständlich implementieren als folgendermaßen?</p>
<pre><code class="language-cpp">struct myspace : public std::unary_function&lt;char, bool&gt;
{
    bool operator ()(char c) const { return std::isspace(c); }
};

std::string trim(std::string str)
{
    typedef std::string::iterator SI;
    typedef std::string::reverse_iterator CSI;

    SI i = std::find_if(str.begin(), str.end(), std::not1(myspace()));
    str.erase(str.begin(), i);

    CSI j = std::find_if(str.rbegin(), str.rend(), std::not1(myspace()));
    str.erase(j.base(), str.end());

    return str;
}
</code></pre>
<p>Irgendwie stört mich die Adapter-struct aber ohne bekomme ich es nicht hin. Gut, der Typ, der die Vorlesung hält, stört meine Konzentration ein wenig. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166059</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Wed, 01 Nov 2006 10:42:33 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 11:01:54 GMT]]></title><description><![CDATA[<p>Um Funktionszeiger zu wrappen, gibt es ptr_fun.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166075</guid><dc:creator><![CDATA[Konstruktör]]></dc:creator><pubDate>Wed, 01 Nov 2006 11:01:54 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Wed, 01 Nov 2006 14:14:09 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cctype&gt;
#include &lt;string&gt;

template&lt;typename TPredicate, typename TContainer&gt;
TContainer trimWhere(const TContainer &amp;container, const TPredicate &amp;condition)
{
    TContainer result;
    size_t offsetFront = 0, offsetBack = 0;

    for(typename TContainer::const_iterator current = container.begin();
        current != container.end() &amp;&amp; condition(*current);
        ++current, ++offsetFront);

    for(typename TContainer::const_reverse_iterator current = container.rbegin();
        current != container.rend() &amp;&amp; condition(*current);
        ++current, ++offsetBack);

    result.assign(container.begin() + offsetFront, container.end() - offsetBack);
    return result;
}

inline std::string trim(const std::string &amp;s)
{
	return trimWhere&lt;int(*)(int)&gt;(s, std::isspace);
}

int main()
{
    std::string test = &quot;  \r hallo \t welt \n &quot;;
    std::cout &lt;&lt; &quot;&gt;&quot; &lt;&lt; trim(test) &lt;&lt; &quot;&lt;&quot; &lt;&lt; std::endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1166264</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166264</guid><dc:creator><![CDATA[schorsch code]]></dc:creator><pubDate>Wed, 01 Nov 2006 14:14:09 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Thu, 02 Nov 2006 08:40:45 GMT]]></title><description><![CDATA[<p>schorsch code schrieb:</p>
<blockquote>
<pre><code class="language-cpp">...
	return trimWhere&lt;int(*)(int)&gt;(s, std::isspace);
...
</code></pre>
</blockquote>
<p>Warum ist der Predicatetype int(*)(int) ?<br />
Im Std steht isspace als</p>
<pre><code class="language-cpp">template &lt;typename charT&gt; bool (*)(charT, const locale&amp;)
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166713</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166713</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 02 Nov 2006 08:40:45 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Thu, 02 Nov 2006 09:47:14 GMT]]></title><description><![CDATA[<p>der einfachheit halber ist es ohne locale. weiß nicht, ob die c-version im standard steht, aber sowohl VC8 als auch g++ 3.4.5 sind damit einverstanden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166768</guid><dc:creator><![CDATA[schorsch code]]></dc:creator><pubDate>Thu, 02 Nov 2006 09:47:14 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Thu, 02 Nov 2006 09:53:02 GMT]]></title><description><![CDATA[<p>Das kommt darauf an, was du einbindest - die &lt;cctype&gt; verpackt afaik alle Funktionen der &lt;ctype.h&gt; in std::</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166774</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166774</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 02 Nov 2006 09:53:02 GMT</pubDate></item><item><title><![CDATA[Reply to leerzeichen entfernen on Thu, 02 Nov 2006 10:41:09 GMT]]></title><description><![CDATA[<p>Eine andere Version aus diesem Forum:</p>
<p>HumeSikkins schrieb:</p>
<blockquote>
<pre><code class="language-cpp">void trimA(std::string&amp; s) {
	typedef int (*Sig)(int);
	s.erase(s.begin(), std::find_if(s.begin(), s.end(), not1(ptr_fun(static_cast&lt;Sig&gt;(isspace)))));
	s.erase(std::find_if(s.rbegin(), s.rend(), not1(ptr_fun(static_cast&lt;Sig&gt;(isspace)))).base(), s.end());
}
</code></pre>
<p>Das ist aber unter Umständen nicht ganz korrekt (siehe char vs. unsigned cahr vs. int Problematik).<br />
Besser also:</p>
<pre><code class="language-cpp">struct IsSpace : std::unary_function&lt;char, bool&gt; {
	bool operator()(char c) const {
		return isspace(static_cast&lt;unsigned char&gt;(c)) != 0;
	}
};

void trim(std::string&amp; s) {
	s.erase(s.begin(), std::find_if(s.begin(), s.end(), not1(IsSpace())));
	s.erase(std::find_if(s.rbegin(), s.rend(), not1(IsSpace())).base(), s.end());
}
</code></pre>
</blockquote>
<p>Grüße,<br />
don_basto.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1166829</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1166829</guid><dc:creator><![CDATA[don_basto]]></dc:creator><pubDate>Thu, 02 Nov 2006 10:41:09 GMT</pubDate></item></channel></rss>