Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Find Number of Repetitions of Substring

using System;
using System.Linq;

public class Program {
	public static int NumberOfRepeats(string str) 
	{	
		int length = str.Length;
		int checkLength = 2;

		while (checkLength < length)
		{
				if (length % checkLength == 0)
				{
						var times = length / checkLength;
						var subStr = str.Substring(0, checkLength);
						var checkStr = string.Concat(Enumerable.Repeat(subStr, times));

						if (checkStr == str)
						{
								return times;
						}
				}

				checkLength++;
		}

		return 1;
	}
}
Source by edabit.com #
 
PREVIOUS NEXT
Tagged: #Find #Number #Repetitions #Substring
ADD COMMENT
Topic
Name
5+5 =