<?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: Manipulating Strings in C# &#8211; Finding all occurrences of a string within another string</title>
	<atom:link href="http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Sat, 04 Jun 2011 10:24:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: Mudasir Khan</title>
		<link>http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/comment-page-1/#comment-11162</link>
		<dc:creator>Mudasir Khan</dc:creator>
		<pubDate>Wed, 18 May 2011 16:03:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=633#comment-11162</guid>
		<description>/// 
        /// Returns the Index of All Occurances of a String
        /// 
        /// Original String
        /// String to Find
        /// 
        public int[] IndexOfAll(String oString, String sString)
        {
            //List of Occurances
            List sOccurances = new List();
            int pos = 0;
            for (int i = 0; i &lt; oString.Length; i++)
            {
                pos = oString.IndexOf(sString, i);
                if (pos != -1)
                {
                    sOccurances.Add(pos);
                    i = pos;
                }
            }
            return (int[])sOccurances.ToArray();
        }</description>
		<content:encoded><![CDATA[<p>///<br />
        /// Returns the Index of All Occurances of a String<br />
        ///<br />
        /// Original String<br />
        /// String to Find<br />
        ///<br />
        public int[] IndexOfAll(String oString, String sString)<br />
        {<br />
            //List of Occurances<br />
            List sOccurances = new List();<br />
            int pos = 0;<br />
            for (int i = 0; i &lt; oString.Length; i++)<br />
            {<br />
                pos = oString.IndexOf(sString, i);<br />
                if (pos != -1)<br />
                {<br />
                    sOccurances.Add(pos);<br />
                    i = pos;<br />
                }<br />
            }<br />
            return (int[])sOccurances.ToArray();<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hoyb</title>
		<link>http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/comment-page-1/#comment-6252</link>
		<dc:creator>Hoyb</dc:creator>
		<pubDate>Mon, 21 Feb 2011 18:37:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=633#comment-6252</guid>
		<description>Wow!

Thanks a ton to Kotelni for finding the bug! I just spent 25 minutes trying to figure out why the code above wouldn&#039;t work right (thanks for the effort though, original poster), but I just pasted Kotelni&#039;s code in, and boom, working indexofall function.  Thanks again!! You rock!</description>
		<content:encoded><![CDATA[<p>Wow!</p>
<p>Thanks a ton to Kotelni for finding the bug! I just spent 25 minutes trying to figure out why the code above wouldn&#8217;t work right (thanks for the effort though, original poster), but I just pasted Kotelni&#8217;s code in, and boom, working indexofall function.  Thanks again!! You rock!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lasse Espeholt</title>
		<link>http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/comment-page-1/#comment-2938</link>
		<dc:creator>Lasse Espeholt</dc:creator>
		<pubDate>Sat, 15 May 2010 12:05:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=633#comment-2938</guid>
		<description>I don&#039;t think these algorithms is very efficient. Instead I think you should look at some algorithms at http://en.wikipedia.org/wiki/List_of_algorithms#Substrings which is designed find multiple substrings instead of using a naive IndexOf loop.

Best regards, Lasse Espeholt</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think these algorithms is very efficient. Instead I think you should look at some algorithms at <a href="http://en.wikipedia.org/wiki/List_of_algorithms#Substrings" >http://en.wikipedia.org/wiki/List_of_algorithms#Substrings</a> which is designed find multiple substrings instead of using a naive IndexOf loop.</p>
<p>Best regards, Lasse Espeholt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guy Ellis</title>
		<link>http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/comment-page-1/#comment-1351</link>
		<dc:creator>Guy Ellis</dc:creator>
		<pubDate>Wed, 16 Sep 2009 14:42:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=633#comment-1351</guid>
		<description>Yes - I can confirm that the comparison must be &gt;= and not &gt; otherwise this will fail in cases where the occurrence is at the start of the string.</description>
		<content:encoded><![CDATA[<p>Yes &#8211; I can confirm that the comparison must be &gt;= and not &gt; otherwise this will fail in cases where the occurrence is at the start of the string.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kotelni</title>
		<link>http://www.dijksterhuis.org/manipulating-strings-in-csharp-finding-all-occurrences-of-a-string-within-another-string/comment-page-1/#comment-993</link>
		<dc:creator>kotelni</dc:creator>
		<pubDate>Fri, 12 Jun 2009 15:54:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=633#comment-993</guid>
		<description>I found IndexOfAll metod call very useful. I like the idea of using yield return.
This is slightly better version of IndexOfAll, I thought it is worth sharing. 
It does not do Substring in a loop which can give a big performance improvement for large strings and (I think) there was a bug in original version that in &#039;while&#039; comparison must be for &gt;=0 not just &gt;0.

protected static IEnumerable IndexOfAll(string haystack, string needle)
{
	int pos;
	int offset = 0;
	int length = needle.Length;
	while ((pos = haystack.IndexOf(needle, offset)) != -1)
	{
		yield return pos;
		offset = pos + length;
	}
}</description>
		<content:encoded><![CDATA[<p>I found IndexOfAll metod call very useful. I like the idea of using yield return.<br />
This is slightly better version of IndexOfAll, I thought it is worth sharing.<br />
It does not do Substring in a loop which can give a big performance improvement for large strings and (I think) there was a bug in original version that in &#8216;while&#8217; comparison must be for &gt;=0 not just &gt;0.</p>
<p>protected static IEnumerable IndexOfAll(string haystack, string needle)<br />
{<br />
	int pos;<br />
	int offset = 0;<br />
	int length = needle.Length;<br />
	while ((pos = haystack.IndexOf(needle, offset)) != -1)<br />
	{<br />
		yield return pos;<br />
		offset = pos + length;<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

