<?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[string::substr() -GELÖST-]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main ()
{
  string s=&quot;abc-efg&quot;;
  string t;

  t = s.substr(s.find('-'));

  cout&lt;&lt;t&lt;&lt;endl;
  return 0;
}
</code></pre>
<p>Substr() liefert mir hier wie gewünscht den Teilstring (&quot;-efg&quot;) zurück.<br />
Falls der String aber kein '-' enthält, stürzt das Programm ab, was unschön ist. Besser wäre die Ausgabe eines Hinweises wie &quot;kein - gefunden&quot;.<br />
Kann man das mit string::substr() bzw. string::find() umsetzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/303998/string-substr-gelöst</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 03:54:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/303998.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 27 May 2012 17:05:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 21:02:19 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main ()
{
  string s=&quot;abc-efg&quot;;
  string t;

  t = s.substr(s.find('-'));

  cout&lt;&lt;t&lt;&lt;endl;
  return 0;
}
</code></pre>
<p>Substr() liefert mir hier wie gewünscht den Teilstring (&quot;-efg&quot;) zurück.<br />
Falls der String aber kein '-' enthält, stürzt das Programm ab, was unschön ist. Besser wäre die Ausgabe eines Hinweises wie &quot;kein - gefunden&quot;.<br />
Kann man das mit string::substr() bzw. string::find() umsetzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216042</guid><dc:creator><![CDATA[redrew99]]></dc:creator><pubDate>Sun, 27 May 2012 21:02:19 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 17:07:35 GMT]]></title><description><![CDATA[<p>find gibt std::string::npos zurück wenn es nichts findet, wie du jeder Referenz hättest entnehmen können..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216044</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 27 May 2012 17:07:35 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 20:22:40 GMT]]></title><description><![CDATA[<p>redrew99 schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main ()
{
  string s=&quot;abc-efg&quot;;
  string t;

  t = s.substr(s.find('-'));

  cout&lt;&lt;t&lt;&lt;endl;
  return 0;
}
</code></pre>
<p>Substr() liefert mir hier wie gewünscht den Teilstring (&quot;-efg&quot;) zurück.<br />
Falls der String aber kein '-' enthält, stürzt das Programm ab, was unschön ist. Besser wäre die Ausgabe eines Hinweises wie &quot;kein - gefunden&quot;.<br />
Kann man das mit string::substr() bzw. string::find() umsetzen?</p>
</blockquote>
<p>Selbstverständlich. Aber leider biste viel zu faul um auf die Seiten im Netz zu gehen und zu lesen, was passiert, wenn <code>basic_string::find()</code> nix findet<br />
...</p>
<pre><code class="language-cpp">if(size_t pos = meinStr.find('-') != std::string::npos)
     //...
else
     std::cout &lt;&lt; &quot;Nix gefunden! :(\n&quot;;
</code></pre>
<p>(ungetestet)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216114</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216114</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 27 May 2012 20:22:40 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 21:20:59 GMT]]></title><description><![CDATA[<p>Danke, da ich jetzt wußte was man sich anschauen muß(string::npos), habe ich selber auch eine Lösung gefunden.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

int main()
{
  string s=(&quot;abc-efg&quot;);
  string t;

  if(s.find(&quot;-&quot;)!=string::npos)
  {
      t=s.substr(s.find('-'));
      cout&lt;&lt;t;
  }
  else
  {
      cout&lt;&lt;&quot;kein Treffer&quot;;
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2216122</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216122</guid><dc:creator><![CDATA[redrew99]]></dc:creator><pubDate>Sun, 27 May 2012 21:20:59 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 20:40:39 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(size_t pos = meinStr.find('-') != std::string::npos)
     //...
else
     std::cout &lt;&lt; &quot;Nix gefunden! :(\n&quot;;
</code></pre>
<p>(ungetestet)</p>
</blockquote>
<p>Glaube, man braucht size_t pos gar nicht, oder?<br />
Der Compiler nimmt auch</p>
<pre><code class="language-cpp">if(meinStr.find('-') != std::string::npos)
     //...
else
     std::cout &lt;&lt; &quot;Nix gefunden! :(\n&quot;;
</code></pre>
<p>anstandslos.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216125</guid><dc:creator><![CDATA[redrew99]]></dc:creator><pubDate>Sun, 27 May 2012 20:40:39 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 20:47:02 GMT]]></title><description><![CDATA[<p>redrew99 schrieb:</p>
<blockquote>
<p>Danke, da ich jetzt wußte was man sich anschauen muß(string::npos), habe ich selber auch eine Lösung gefunden.</p>
</blockquote>
<p>Mit found hast du doch im Falle von ungleich string:npos den Index, kein Grund find ein zweites Mal zu bemühen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216130</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216130</guid><dc:creator><![CDATA[mapper]]></dc:creator><pubDate>Sun, 27 May 2012 20:47:02 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 21:27:33 GMT]]></title><description><![CDATA[<p>Stimmt^^, also dann z.B.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

int main()
{
  string s=(&quot;abc-efg&quot;);
  string t;
  size_t found;
  found=s.find(&quot;-&quot;);
  if(found!=string::npos)
  {
      t=s.substr(found);
      cout&lt;&lt;t;
  }
  else
  {
      cout&lt;&lt;&quot;kein Treffer&quot;;
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2216152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216152</guid><dc:creator><![CDATA[redrew99]]></dc:creator><pubDate>Sun, 27 May 2012 21:27:33 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Sun, 27 May 2012 21:31:59 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(size_t pos = meinStr.find('-') != std::string::npos)
</code></pre>
</blockquote>
<p>Löl, ich liebe Code-Snippets von Hacker.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216154</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216154</guid><dc:creator><![CDATA[agkma]]></dc:creator><pubDate>Sun, 27 May 2012 21:31:59 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 06:43:48 GMT]]></title><description><![CDATA[<p>redrew99 schrieb:</p>
<blockquote>
<p>Hacker schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(size_t pos = meinStr.find('-') != std::string::npos)
     //...
else
     std::cout &lt;&lt; &quot;Nix gefunden! :(\n&quot;;
</code></pre>
<p>(ungetestet)</p>
</blockquote>
<p>Glaube, man braucht size_t pos gar nicht, oder?<br />
Der Compiler nimmt auch</p>
<pre><code class="language-cpp">if(meinStr.find('-') != std::string::npos)
     //...
else
     std::cout &lt;&lt; &quot;Nix gefunden! :(\n&quot;;
</code></pre>
<p>anstandslos.</p>
</blockquote>
<p>Vielleicht willst du ja auch wissen, wo der Buchstabe gefunden wurde, oder? XD<br />
Ja, das war jetzt falsch, denn es muss so aussehen;</p>
<pre><code class="language-cpp">size_t pos(meinStr.find('-'));
if(pos != std::string::npos)
</code></pre>
<p>Ach, noch ein paar kleine Design-Tipps:</p>
<p>redrew99 schrieb:</p>
<blockquote>
<p>Stimmt^^, also dann z.B.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

int main()
{
  string s=(&quot;abc-efg&quot;);//Das sieht komisch aus, meintest du nicht eher std::string s(&quot;abc-efg&quot;); ?
  string t;//Wieso hier? Mach es doch da, wo du es brauchst!
  size_t found;//Mach doch das
  found=s.find(&quot;-&quot;);//Und das in einer Anweisung!
  if(found!=string::npos)
  {
      t=s.substr(found);//So, hier könntest du jetzt t deklarieren!
      cout&lt;&lt;t;//Und das bräuchtest du eig. gar nicht, oder?
  }
  else
  {
      cout&lt;&lt;&quot;kein Treffer&quot;;
  }
}
</code></pre>
</blockquote>
<p>Die ganze main() könnte also so aussehen:</p>
<pre><code class="language-cpp">string s(&quot;abc-efg&quot;);
  size_t found(s.find(&quot;-&quot;));

  if(found != string::npos)
      cout &lt;&lt; s.substr(found);
  else
      cout&lt;&lt;&quot;kein Treffer&quot;;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2216206</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216206</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 28 May 2012 06:43:48 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 10:18:38 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<pre><code class="language-cpp">string s(&quot;abc-efg&quot;); // Warum ein std::string, wenn wir ihn im ganzen Programm nie wieder bearbeiten wollen? Wie auch immer, auf jeden Fall consten. Außerdem ist ()-Initialisierung bei strings voll hässlich.
  size_t found(s.find(&quot;-&quot;)); // Auch found wird nicht mehr verändert, also auch const.
// Des Weiteren suggeriert der Name &quot;found&quot;, dass die Variable vom Typ bool ist, wir nennen sie also lieber &quot;pos&quot;.
// string::find ist außerdem uncool, weil es die Funktion schon in &lt;algorithm&gt; als std::find gibt.
// Achja: ()-Initialisierungssytax bei Integern gefällt PI überhaupt nicht. Das sieht ihm zu zwanghaft aus.

  if(found != string::npos)
      cout &lt;&lt; s.substr(found); // Näh. Vieeeeeeeeel zu ineffizient. Wozu extra Speicher allozieren, wenn wir nur von Position X bis Ende ausgeben wollen.
  else
      cout&lt;&lt;&quot;kein Treffer&quot;; // Und gewöhn dir bitte mal nen einheitlichen Leerzeichen-setzungs-Stil an. Ist ja grauenhaft.
</code></pre>
</blockquote>
<p>Mal alles Gesagte geändert:</p>
<pre><code class="language-cpp">string const s = &quot;abc-efg&quot;;
auto const pos = find(s.begin(), s.end(), '-');

if(pos != s.end())
    copy(pos, s.end(), ostream_iterator&lt;char&gt;(cout));
else
    cout &lt;&lt; &quot;kein Treffer&quot;;
</code></pre>
<p>Aber das ist doch viel zu umständlich!<br />
Also mal std::string durch char const* ersetzen.</p>
<pre><code class="language-cpp">char const s[] = &quot;abc-efg&quot;;
auto const pos = find(begin(s), end(s), '-');

cout &lt;&lt; (pos != end(s) ? pos : &quot;kein Treffer&quot;); // höhö
</code></pre>
<p>Win!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216256</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216256</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 28 May 2012 10:18:38 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 10:56:39 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Also mal std::string durch char const* ersetzen.</p>
</blockquote>
<p>Ne, nicht Win. Der String ist dem OP offensichtlich nicht im Voraus bekannt, wie es bei diesem Minimalbeispiel der Fall ist. Du willst also das komplette Hantieren mit einer unbekannten Zeichenkette von std::string auf const char* umstellen, nur um ein if durch den ternären Operator zu ersetzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216264</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216264</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Mon, 28 May 2012 10:56:39 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 10:58:27 GMT]]></title><description><![CDATA[<p>Nö. Man braucht hier nichts allozieren, deshalb ersetze ich. Dass das konstrukt kürzer wird, ist ein netter Nebeneffekt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216267</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216267</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 28 May 2012 10:58:27 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 11:42:03 GMT]]></title><description><![CDATA[<p>Naja, um ganz ehrlich zu sein, hab ich den Code vom OP nicht verändert, sondern nur geschnitten. <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="😉"
    /><br />
C++11 hat der auch nicht umbedingt, und das consten sehe ich hier als unnötig und damit übrflüssige 5 Buchstaben. Klammer-Initialisierung find ich sexy, und was den Ternären Operator angeht: Wenn wir hier einen auf so effizient wie möglich gemacht hätten, also so performant wie nur irgendmöglich - dann hätt' ich den Code auch modifiziert <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/2216279</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216279</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 28 May 2012 11:42:03 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 11:45:28 GMT]]></title><description><![CDATA[<p>Klammer-Initialisierung sieht total behindert aus. Du machst das ja auch bei ints und so.</p>
<pre><code class="language-cpp">int i(0);
</code></pre>
<p>Das ist nicht nur mehr Tipparbeit, sondern verwirrt auch nur.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216280</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 28 May 2012 11:45:28 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 12:03:17 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Das ist nicht nur mehr Tipparbeit [...]</p>
</blockquote>
<p>Wie das denn ? () gegen ' ' = ' ' .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216284</guid><dc:creator><![CDATA[Joa?]]></dc:creator><pubDate>Mon, 28 May 2012 12:03:17 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 12:04:43 GMT]]></title><description><![CDATA[<p>Abstände Tippen sich leichter als Sonderzeichen, bei denen man die Umschalttaste braucht. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216285</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216285</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 28 May 2012 12:04:43 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 13:29:13 GMT]]></title><description><![CDATA[<p>Ein '=' ist auch ein Sonderzeichen. () liegen direkt nebeneinander. Manche IDE's ergänzen sogar die ) automatisch zu ( und setzten den Cursor dazwischen.<br />
Die Zahlen liegen tatsächlich auch dort, wo die Sonderzeichen sind. Abstände sind also nicht leichter zu tippen <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/2216301</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216301</guid><dc:creator><![CDATA[Joa?]]></dc:creator><pubDate>Mon, 28 May 2012 13:29:13 GMT</pubDate></item><item><title><![CDATA[Reply to string::substr() -GELÖST- on Mon, 28 May 2012 13:43:52 GMT]]></title><description><![CDATA[<p>Ich denke mal das Problem ist gelöst. Um Haarspaltereien und Geschmacksfragen braucht man sich hier nicht zu streiten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2216308</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2216308</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 28 May 2012 13:43:52 GMT</pubDate></item></channel></rss>