Как в RichTextBox взять первые n символов, учитывая что может быть форматирование текста
Здравствуйте, dsalodki, Вы писали:
D>Как в RichTextBox взять первые n символов, учитывая что может быть форматирование текста
string StringFromRichTextBox(RichTextBox rtb)
{
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
rtb.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
rtb.Document.ContentEnd
);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}
Здравствуйте, vaa, Вы писали:
vaa>Здравствуйте, dsalodki, Вы писали:
D>>Как в RichTextBox взять первые n символов, учитывая что может быть форматирование текста
vaa>vaa>string StringFromRichTextBox(RichTextBox rtb)
vaa>{
vaa> TextRange textRange = new TextRange(
vaa> // TextPointer to the start of content in the RichTextBox.
vaa> rtb.Document.ContentStart,
vaa> // TextPointer to the end of content in the RichTextBox.
vaa> rtb.Document.ContentEnd
vaa> );
vaa> // The Text property on a TextRange object returns a string
vaa> // representing the plain text content of the TextRange.
vaa> return textRange.Text;
vaa>}
vaa>
не совсем то что нужно, там могут быть непечатные символы, но решение нашёл в интернете
https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically
подскажи пожалуйста как из TextRange задать форматированный текст у RichTextBox
D> не совсем то что нужно, там могут быть непечатные символы, но решение нашёл в интернете
D>https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically
D>подскажи пожалуйста как из TextRange задать форматированный текст у RichTextBox
вроде нашёл
TextRange allText = new TextRange(rtfMain.Document.ContentStart, rtfMain.Document.ContentEnd);
FileStream stream = new FileStream(@"D:\AboutMyDog.xaml", FileMode.Create);
allText.Save(stream, DataFormats.Xaml);
//......................
// select all
rtfMain.Selection.Load(new FileStream(@"D:\Spade.rtf", FileMode.Open), DataFormats.Rtf);
типа такого
Здравствуйте,
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 в данном случае