Answer the question
In order to leave comments, you need to log in
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
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question