C#:到达100个字符后,如何在空白处拆分字符串

C#:到达100个字符后,如何在空白处拆分字符串,c#,string,foreach,split,C#,String,Foreach,Split,我有以下代码,但它似乎没有做它应该做的。foreach循环中的代码需要遍历每个字符,直到第100个字符。但循环似乎在继续,并没有停止。我该如何解决这个问题 StringBuilder builder = new StringBuilder(); string[] a = MainRichTextBox.Text.Split('.'); while (a[0].Length > 0 || a[0].Length != 0) { NumberOfChars = a[0].Length

我有以下代码,但它似乎没有做它应该做的。foreach循环中的代码需要遍历每个字符,直到第100个字符。但循环似乎在继续,并没有停止。我该如何解决这个问题

StringBuilder builder = new StringBuilder();
string[] a = MainRichTextBox.Text.Split('.');

while (a[0].Length > 0 || a[0].Length != 0)
{
    NumberOfChars = a[0].Length;

    if (NumberOfChars < 100)
    {
        r = NumberOfChars;

    }

    else if (NumberOfChars == 100)
    {
        r = 100;
    }

    else if (NumberOfChars > 100)
    {
        r = 100;
    }

    decimal numberOfTweetsUnRounded = a[0].Length / 100M;

    if (numberOfTweetsUnRounded == 0)
    {
        numberOfTweetsUnRounded = 1;
    }

    int numberOfTweetsRounded = Convert.ToInt32(Math.Ceiling(numberOfTweetsUnRounded));
    int numberOfSpaces = 0;
    int indexOfSpaces = 0;
    int indexOfCChars = 0;
    int indexPointOfSplit = 0;
    for (int i = 1; i <= numberOfTweetsRounded; i++)
    {
        if (r == 100)
        {
            //The problem occurs here. This loop doesn't stop at the 100th character.
            foreach (char c in a[0].Substring(0, 100))
            {
                indexOfCChars++;
                if (c == ' ')
                {
                    indexOfSpaces++;
                    indexPointOfSplit = indexOfCChars;
                    MessageBox.Show(indexPointOfSplit.ToString());
                }
            }
        }

        //When I split the string, it doesn't work. It gives an error of the index being outside the range.
        string breakOff = a[0].Substring(0, indexPointOfSplit);
        builder.Append(breakOff).Append(" " + textBox1.Text).Append(" [" + i + "/" + numberOfTweetsRounded + "] " + "\r\n");

        showNow = builder.ToString();
    }

    a[0] = a[0].Remove(0, indexPointOfSplit);
    builder.Clear();
    NumberOfChars = a[0].Length;
    MainRichTextBox.AppendText(showNow);


    //When I split the string, it doesn't work. It gives an error of the index being outside the range.
    string breakOff = a[0].Substring(0, indexPointOfSplit);
    builder.Append(breakOff).Append(" " + textBox1.Text).Append(" [" + i + "/" + numberOfTweetsRounded + "] " + "\r\n");

    showNow = builder.ToString();
}

a[0] = a[0].Remove(0, indexPointOfSplit);
builder.Clear();
NumberOfChars = a[0].Length;
MainRichTextBox.AppendText(showNow);
StringBuilder=新建StringBuilder();
字符串[]a=MainRichTextBox.Text.Split('.');
while(a[0]。长度>0 | | a[0]。长度!=0)
{
NumberOfChars=a[0]。长度;
如果(字符数<100)
{
r=字符数;
}
否则如果(NumberOfChars==100)
{
r=100;
}
否则如果(NumberOfChars>100)
{
r=100;
}
小数位数修约=a[0]。长度/100M;
如果(numberOfTweetsUnRounded==0)
{
numberOfTweetsUnRounded=1;
}
int numberOfTweetsRounded=Convert.ToInt32(数学上限(numberOfTweetsUnRounded));
int numberOfSpaces=0;
int indexOfSpaces=0;
int indexOfCChars=0;
int indexPointOfSplit=0;

对于(inti=1;i好吧,我逐步完成了代码,它似乎退出了那个循环,我对您的代码做了一些轻微的更改:

  while (a[0].Length > 0 || a[0].Length != 0)
        {
            NumberOfChars = a[0].Length;
            if (NumberOfChars < 100)
            {
                r = NumberOfChars;

            }
            else if (NumberOfChars == 100)
            {
                r = 100;
            }
            else if (NumberOfChars > 100)
            {
                r = 100;
            }

            decimal numberOfTweetsUnRounded = a[0].Length/100M; 
            if (numberOfTweetsUnRounded == 0)
            {
                numberOfTweetsUnRounded = 1;
            }

            int numberOfTweetsRounded = Convert.ToInt32(Math.Ceiling(numberOfTweetsUnRounded));
            int numberOfSpaces = 0;
            int indexOfSpaces = 0;
            int indexPointOfSplit = 0;
            for (int i = 1; i <= numberOfTweetsRounded; i++)
            {
                if (r == 100)
                {
                    string firstItem = a[0].Substring(0, 100);
                    for (int pos = 0; pos < firstItem.Length; pos++)
                    {
                        if (!Char.IsWhiteSpace(firstItem[pos]))
                            continue;
                        indexOfSpaces++;
                        indexPointOfSplit = pos;
                    }
                }
                //When I split the string, it doesn't work. It gives an error of the index being outside the range.
                string breakOff = a[0].Substring(0, indexPointOfSplit);
            }
            a[0] = a[0].Remove(0, indexPointOfSplit);
            builder.Clear();
            NumberOfChars = a[0].Length;
        }
while(a[0].长度>0 | | a[0].长度!=0)
{
NumberOfChars=a[0]。长度;
如果(字符数<100)
{
r=字符数;
}
否则如果(NumberOfChars==100)
{
r=100;
}
否则如果(NumberOfChars>100)
{
r=100;
}
小数位数修约=a[0]。长度/100M;
如果(numberOfTweetsUnRounded==0)
{
numberOfTweetsUnRounded=1;
}
int numberOfTweetsRounded=Convert.ToInt32(数学上限(numberOfTweetsUnRounded));
int numberOfSpaces=0;
int indexOfSpaces=0;
int indexPointOfSplit=0;

对于(int i=1;i我认为您希望在
foreach
循环之前将
indexOfCChars
初始化为0。因为它在循环之前的值可能大于0,所以在循环期间可以增加到大于100的值。这使得循环体似乎执行了100次以上,这是不可能的。

您需要在if(r==100)语句之后设置indexOfCChars=0

 if (r == 100)
    {

        indexOfCChars = 0

        //The problem occurs here. This loop doesn't stop at the 100th character.
        foreach (char c in a[0].Substring(0, 100))
        {
            indexOfCChars++;
            if (c == ' ')
            {
                indexOfSpaces++;
                indexPointOfSplit = indexOfCChars;
                MessageBox.Show(indexPointOfSplit.ToString());
            }
        }
    }

它停在什么位置?是否尝试单步执行代码?如果使用小于100的字符串进行测试,则
a[0]。子字符串(0,100)
将爆炸。该
foreach
没有问题。如果子字符串()不抛出
ArgumentOutOfRangeException
。在Java中已经有相同的问题和答案。我相信您可以从该解决方案中获得一些东西: