简体   繁体   中英

RichTextBox line break after n characters

Is there a way to break the line in a RichTextBox after a specific amount of characters without the string having a new line? (In this example case 5 characters but in my real code after 80 characters)

//Rendered as
Examp 
le Te
xt  

//Actual String
Console.WriteLine(richTextBox1.Text) //"Example Text"

I am highlighting a specific phrase in a it. For example "Text" is being highlighted and it only works if the string is "Example Text" and not "Examp\\nle Te\\nxt"

I've also tried Replacing "\\n" with "" and then readjusting the positions because '\\n' counts as a character but I could not get it to work

Instead of using "\\n" try replacing Enviroment.NewLine with "" and then readjusting the positions.

string Text = richTextBox1.Text;
Text = Text.Replace(Enviroment.NewLine, "");

string NewText = "";
for(int i = 0; i < Text.Length; i++){

    if( i % 5 == 0)// Insert a line break every 5 characters
        NewText += Enviroment.NewLine;

    NewText += Text[i];
}

If you need maxium performance you may want to use a StringBuilder or take a look into String.Insert

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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