简体   繁体   English

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

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

I have got the following code but it doesn't seem to do what it should do. 我有以下代码,但它似乎没有执行应做的事情。 The code at the foreach loop needs to go through each character up to the 100th character. foreach循环中的代码需要遍历每个字符,直至第100个字符。 But it appears the loop runs on, it doesn't stop. 但似乎循环继续进行,并没有停止。 How do I fix that? 我该如何解决?

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

Well, I stepped throught he code, and and it appears to exit that loop, I made some slight changes to your code: 好吧,我遍历了他的代码,并且似乎退出了该循环,我对您的代码做了一些细微的更改:

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

Hope that helps. 希望能有所帮助。

I think you want to initialize indexOfCChars to 0 before the foreach loop. 我认为您想在foreach循环之前将indexOfCChars初始化为0。 Since it can have a value greater than 0 before the loop, it can be incremented to a value greater than 100 during the loop. 由于它可以在循环之前具有大于0的值,因此可以在循环期间将其增大为大于100的值。 This makes it appear as if the loop body is executing over 100 times, which is not likely. 这使得循环体似乎执行了100多次,这不太可能。

I believe you need to set indexOfCChars = 0 after your if (r == 100) statement. 我相信您需要在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