Skip to content

AS3 – Find all occurrences of a string in another string

function findOccurrences(needle:String, haystack:String):Array
{
	var index:int = 0;
	var occurrences:Array = [];
	
	while(haystack.indexOf(needle, index) != -1)
	{
		var pos:int = haystack.indexOf(needle, index);
		occurrences.push(pos);
		
		index = pos + 1;
	}
	
	return occurrences;
}

source

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*