📌  相关文章
📜  查找子字符串的重复次数 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:20.304000             🧑  作者: Mango

代码示例1
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;
    }
}