繁体   English   中英

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

[英]C#: How do i split a string on a whitespace after 100 characters have been reached

我有以下代码,但它似乎没有执行应做的事情。 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);

好吧,我遍历了他的代码,并且似乎退出了该循环,我对您的代码做了一些细微的更改:

  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;
        }

希望能有所帮助。

我认为您想在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());
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM