Re[4]: WPF RichTextBox взять первые 20000 символов
От: MonsterZam СССР  
Дата: 12.05.23 05:54
Оценка:
Здравствуйте,

D>> не совсем то что нужно, там могут быть непечатные символы, но решение нашёл в интернете


D>>https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically


D>>подскажи пожалуйста как из TextRange задать форматированный текст у RichTextBox


ChatGPT предлагает
1) читаем всё в переменную используя File.ReadAllText
2) используя RegEx удаляем все форматирующие последовательности

string plainText = Regex.Replace(rtfText, @"\\[^A-Za-z0-9\s]+|\\[A-Za-z]+\d+\\|\\u\d+?", string.Empty);

This regular expression matches two types of patterns: \[^A-Za-z0-9\s]+, which matches any backslash followed by one or more characters that are not letters, digits, or whitespace; and \[A-Za-z]+\d+\\, which matches any backslash followed by one or more letters, one or more digits, and a final backslash. This will remove most RTF formatting commands and escape sequences, but it will leave printable characters and non-RTF-specific control characters intact.

Note that some non-printable characters may still be present in the output if they are not encoded using RTF-specific escape sequences. If you need to remove all non-printable characters, you can modify the regular expression to include additional patterns that match specific Unicode ranges or control characters.

3) из результата, полученного во 2о шага, просто читаем необходимое кол-во символов. 20K в данном случае
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.