划分给定字符串的元素

时间:2019-03-01 20:29:50

标签: c# list

我应该将元素划分为几个具有等长的小子字符串。 子字符串的数量应等于给定的分区。如果字符串不能精确地划分到给定的分区中,则我必须使除最后一个长度相等的分区以外的所有分区,并使最后一个分区–最长< / strong>。

我已尝试制作第一部分,但并非在所有情况下都有效。您能给我展示一种方法,但是如果有可能使用for循环等?

例如: {abcd},3个分区-> {a,b,cd}; {qrstuvwxyz},5个分区-> {qr st uv wx yz}

private static List<string> Divide(List<string> input, int index, int partitions)
{
        string stringToDivide = input[index];
        input.RemoveAt(index);
        string add = "";

        if (stringToDivide.Length % partitions == 0)
        {

            for (int i = 0; i < stringToDivide.Length; i++)
            {
                add += stringToDivide[i] + " ";
            }

            input.Insert(index, add.Trim());
        }
        else
        {

        }
        return input;
        Console.WriteLine(string.Join(' ', input));
}

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

private static List<string> Divide(List<string> input, int index, int partitions)
{
    var stringToDivide = input[index];
    input.RemoveAt(index);

    var stringToAdd = "";
    var partitionLength = stringToDivide.Length / partitions;

    for (int i = 0, partitionNum = 0; i < stringToDivide.Length; i++)
    {
        if (i % partitionLength == 0 && partitionNum != partitions)  // skip space in last part
        {
            if (i > 0) // do not add leading space
            {
                stringToAdd += " ";
            }

            partitionNum++;
        }

        stringToAdd += stringToDivide[i];
    }

    input.Insert(index, stringToAdd);

    return input;
}

此代码为每个partitionLength添加空格,但跳过最后一个字符串的空格。请注意,对于长字符串,最好使用StringBuilder

答案 1 :(得分:0)

stringToDivide.Length % partitions是划分为partitions个分区后剩余的字符数。

stringToDivide.Length / partitions是每个分区(最后一个分区除外)中应包含的字符数,最后一个分区上应附加左侧字符。

因此,只需提取第partitions个长度为stringToDivide.Length / partitions的块,然后将剩余的块附加到最后一个块上即可。

当字符串可均分时,stringToDivide.Length % partitions为零,因此不是特殊情况。

答案 2 :(得分:-1)

 public static List<string> DivideIntoPartitions(string stringToDivide, int partitions)
        {
            var parts = new List<string>(partitions);
            var len = stringToDivide.Length;

            if (len < partitions)
            {
                throw new ArgumentException("partitions should be less than length");
            }

            if (len % partitions == 0)
            {
                var eachSubstrLength = len / partitions;

                for (int i = 0; i < stringToDivide.Length; i += eachSubstrLength)
                {
                    parts.Add(stringToDivide.Substring(i, eachSubstrLength));
                }
            }
            else
            {
                var nextDivisibleNumber = len + (partitions - (len % partitions));
                var lengthOfLastSubstr = nextDivisibleNumber / partitions;
                var lastItem = stringToDivide.Substring((len - lengthOfLastSubstr));
                stringToDivide = stringToDivide.Remove((len - lengthOfLastSubstr));

                var chunksize = stringToDivide.Length / (partitions - 1);

                for (int i = 0; i < stringToDivide.Length; i += chunksize)
                {
                    parts.Add(stringToDivide.Substring(i, chunksize));
                }
                parts.Add(lastItem);

            }
            return parts;

        }

 var result = DivideIntoPartitions("qrstuvwxyz", 3);