<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to Trim Leading or Trailing Spaces of a String in C++?</title>
	<atom:link href="http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/</link>
	<description>on programming tips and trending topics...</description>
	<lastBuildDate>Sun, 29 Jan 2012 09:15:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: asoko</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2024</link>
		<dc:creator>asoko</dc:creator>
		<pubDate>Tue, 03 Nov 2009 00:39:30 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2024</guid>
		<description>There&#039;s always Boost (http://www.boost.org/doc/libs/1_40_0/doc/html/string_algo/usage.html#id1685502):

string phone=&quot;00423333444&quot;;
/ remove leading 0 from the phone number
trim_left_if(phone,is_any_of(&quot;0&quot;)); // phone == &quot;423333444&quot;</description>
		<content:encoded><![CDATA[<p>There&#8217;s always Boost (<a href="http://www.boost.org/doc/libs/1_40_0/doc/html/string_algo/usage.html#id1685502" rel="nofollow">http://www.boost.org/doc/libs/1_40_0/doc/html/string_algo/usage.html#id1685502</a>):</p>
<p>string phone=&#8221;00423333444&#8243;;<br />
/ remove leading 0 from the phone number<br />
trim_left_if(phone,is_any_of(&#8220;0&#8243;)); // phone == &#8220;423333444&#8243;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: japplegame</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2026</link>
		<dc:creator>japplegame</dc:creator>
		<pubDate>Sat, 01 Aug 2009 09:28:16 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2026</guid>
		<description>string trim(const string&amp; str) {
  size_t start = str.find_first_not_of(&quot; \t\n\r&quot;);
  if(start == string::npos) return &quot;&quot;;
  return str.substr(start, str.find_last_not_of(&quot; \t\n\r&quot;) - start + 1);
}</description>
		<content:encoded><![CDATA[<p>string trim(const string&amp; str) {<br />
  size_t start = str.find_first_not_of(&#8221; \t\n\r&#8221;);<br />
  if(start == string::npos) return &#8220;&#8221;;<br />
  return str.substr(start, str.find_last_not_of(&#8221; \t\n\r&#8221;) &#8211; start + 1);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sabba</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2025</link>
		<dc:creator>sabba</dc:creator>
		<pubDate>Sun, 12 Jul 2009 06:17:18 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2025</guid>
		<description>Here&#039;s another approach:
http://mlawire.blogspot.com/2009/07/c-whitespace-trimming-functions.html</description>
		<content:encoded><![CDATA[<p>Here&#8217;s another approach:<br />
<a href="http://mlawire.blogspot.com/2009/07/c-whitespace-trimming-functions.html" rel="nofollow">http://mlawire.blogspot.com/2009/07/c-whitespace-trimming-functions.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: How to programmatically create your tiny URL? &#171; Sharing my thoughts&#8230;</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2023</link>
		<dc:creator>How to programmatically create your tiny URL? &#171; Sharing my thoughts&#8230;</dc:creator>
		<pubDate>Mon, 02 Mar 2009 14:37:55 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2023</guid>
		<description>[...] http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/ [...]</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/" rel="nofollow">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JohnK</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2022</link>
		<dc:creator>JohnK</dc:creator>
		<pubDate>Thu, 11 Dec 2008 16:04:33 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2022</guid>
		<description>Thanks!</description>
		<content:encoded><![CDATA[<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: S a r a t h</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2017</link>
		<dc:creator>S a r a t h</dc:creator>
		<pubDate>Fri, 18 May 2007 23:25:34 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2017</guid>
		<description>You can pass any string to find_first_not_of or find_last_not_of function. Here I&#039;ve used a space and tabe ( ” \t”) . It&#039;s you application&#039;s logic to choose between the strings. If you want to replace back slash string should be &quot;\\&quot; and if forward slash, it should be &quot;/&quot; and whatever you like to search.</description>
		<content:encoded><![CDATA[<p>You can pass any string to find_first_not_of or find_last_not_of function. Here I&#8217;ve used a space and tabe ( ” \t”) . It&#8217;s you application&#8217;s logic to choose between the strings. If you want to replace back slash string should be &#8220;\\&#8221; and if forward slash, it should be &#8220;/&#8221; and whatever you like to search.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: arunmozhi</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2018</link>
		<dc:creator>arunmozhi</dc:creator>
		<pubDate>Fri, 18 May 2007 11:56:29 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2018</guid>
		<description>How to Trim leading and Trailing zero &amp; slash of a String in C++</description>
		<content:encoded><![CDATA[<p>How to Trim leading and Trailing zero &amp; slash of a String in C++</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Martin</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2019</link>
		<dc:creator>Jeff Martin</dc:creator>
		<pubDate>Sun, 04 Mar 2007 06:15:11 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2019</guid>
		<description>Looks much better. :-)  BTW, you don&#039;t really need to test endpos, because if the string is empty (zero length), find_first_not_of() will return npos.  And if the string is entirely spaces, it will also return npos.  In both cases, testing startpos is sufficient.  In fact, if startpos is npos, then you don&#039;t even need to compute endpos with find_last_not_of().  On the other hand, in both cases where startpos is npos, empty string and all spaces, find_last_not_of() will be very very fast.  So it would be only a very minor savings to avoid the find_last_not_of().

Performance of operator=() vs resize() depends on your implementation of std::string.  If you can see the internals of your std::string implementation, then you can use whichever is faster.  My guess is that most implementation of std::string will be very close performance-wise between the two.  I&#039;m using the impl that comes with the Solaris compilers, but I haven&#039;t checked to see what&#039;s fastest for that one.

FYI, regarding your popularity, I found you via a Google search on: &quot;C++&quot; string trim spaces.  This was the 7th hit. It&#039;s funny how Google can bump up your site traffic.  I had a similar experience with the photography portion of my own website, netjeff.com.  For the first 4 months of 2006, for some reason my site was the #1 result on images.google.com for the search term &quot;clouds&quot;.  That increased my page hits by more than 500%.  But now I&#039;m not even on the first page of hits, and my traffic is back to my usual.</description>
		<content:encoded><![CDATA[<p>Looks much better. <img src='http://codereflect.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   BTW, you don&#8217;t really need to test endpos, because if the string is empty (zero length), find_first_not_of() will return npos.  And if the string is entirely spaces, it will also return npos.  In both cases, testing startpos is sufficient.  In fact, if startpos is npos, then you don&#8217;t even need to compute endpos with find_last_not_of().  On the other hand, in both cases where startpos is npos, empty string and all spaces, find_last_not_of() will be very very fast.  So it would be only a very minor savings to avoid the find_last_not_of().</p>
<p>Performance of operator=() vs resize() depends on your implementation of std::string.  If you can see the internals of your std::string implementation, then you can use whichever is faster.  My guess is that most implementation of std::string will be very close performance-wise between the two.  I&#8217;m using the impl that comes with the Solaris compilers, but I haven&#8217;t checked to see what&#8217;s fastest for that one.</p>
<p>FYI, regarding your popularity, I found you via a Google search on: &#8220;C++&#8221; string trim spaces.  This was the 7th hit. It&#8217;s funny how Google can bump up your site traffic.  I had a similar experience with the photography portion of my own website, netjeff.com.  For the first 4 months of 2006, for some reason my site was the #1 result on images.google.com for the search term &#8220;clouds&#8221;.  That increased my page hits by more than 500%.  But now I&#8217;m not even on the first page of hits, and my traffic is back to my usual.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: S a r a t h</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2021</link>
		<dc:creator>S a r a t h</dc:creator>
		<pubDate>Sat, 03 Mar 2007 04:33:18 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2021</guid>
		<description>Jeff,
Thanks a lot for pointing out the mistake. At that time i never thought about error checking. but now a days this is one of most visited post in my blog. See the update code. I think operator = is faster tha resize() function since it is doing some relocation but at the time of assignment, it&#039;s managing the new string within allocated memory in this case. Anyway please reply back me if I said something wrong.

Once again thanks alot for pointing out the errors.</description>
		<content:encoded><![CDATA[<p>Jeff,<br />
Thanks a lot for pointing out the mistake. At that time i never thought about error checking. but now a days this is one of most visited post in my blog. See the update code. I think operator = is faster tha resize() function since it is doing some relocation but at the time of assignment, it&#8217;s managing the new string within allocated memory in this case. Anyway please reply back me if I said something wrong.</p>
<p>Once again thanks alot for pointing out the errors.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Martin</title>
		<link>http://codereflect.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/comment-page-1/#comment-2020</link>
		<dc:creator>Jeff Martin</dc:creator>
		<pubDate>Sat, 03 Mar 2007 00:41:42 +0000</pubDate>
		<guid isPermaLink="false">http://sarathc.wordpress.com/2007/01/31/how-to-trim-leading-or-trailing-spaces-of-string-in-c/#comment-2020</guid>
		<description>You need to check that start != npos.   This occurs when the input string consists entirely of spaces.   If you don&#039;t check, and you send in all spaces, you get undefined behavior out of the substr() call.  When start == npos, you can simply str.resize(0)</description>
		<content:encoded><![CDATA[<p>You need to check that start != npos.   This occurs when the input string consists entirely of spaces.   If you don&#8217;t check, and you send in all spaces, you get undefined behavior out of the substr() call.  When start == npos, you can simply str.resize(0)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

