M
M
Mikhail2017-09-25 16:30:54
C++ / C#
Mikhail, 2017-09-25 16:30:54

C# How to make a line break after a certain number of characters in a string?

There is a text that needs to be searched every 300 characters for a comma, period, space, etc. .d) did a line break and so on.
I want to display this through a Label, I know that there is an option with a Textbox, but it does not suit me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Arxont, 2017-09-27
@TheMaxai

Try this (for example, I took the text of your question and the line size is 50, not 300)

string sourceString = "Есть текст нужно чтобы. через каждые 300 символов искал запятую, точку, пробел и т.д (Искал не после 300 символов а именно в этом отрезке 0 - 300 или например 300 - 600) и после этого символа(запятой, точки, пробела и т.д.) делал перенос строки и так далее. Я хочу вывести это через Label, я знаю что есть вариант с Textbox но он мне не подходит.";
int length = 50;
char[] charsForEOL = { '.', ',', ' ' };
string resultString = "";

label1.Text = sourceString;

do
{
    for (int i = length; i >= 1; i--)
    {
        if (charsForEOL.Contains(sourceString[i]))
        {
                resultString += sourceString.Substring(0, i) + "\n";
                sourceString = sourceString.Substring(i + 1);
                break;
        }
        if(i == 1)
        {
                resultString += sourceString.Substring(0, length) + "\n";
                sourceString = sourceString.Substring(length + 1);
        }
    }
} while (sourceString.Length > length);

resultString += sourceString;

label2.Text = resultString;

21c9fc023e8fd6f98d8cf43aa41f8ef261b216e6

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question