C# 使用正则表达式时出错(验证正则表达式的代码)
我有这段代码C# 使用正则表达式时出错(验证正则表达式的代码),c#,regex,wpf,C#,Regex,Wpf,我有这段代码 string myText = new TextRange(mainWindow.richtextbox2.Document.ContentStart, mainWindow.richtextbox2.Document.ContentEnd).Text; //replace two or more consecutive spaces with a single space, and //replace two or more c
string myText = new TextRange(mainWindow.richtextbox2.Document.ContentStart,
mainWindow.richtextbox2.Document.ContentEnd).Text;
//replace two or more consecutive spaces with a single space, and
//replace two or more consecutive newlines with a single newline.
var str = Regex.Replace(myText, @"( |\r?\n)\1+", "$1", RegexOptions.Multiline);
mainWindow.Dispatcher.Invoke(new Action(() =>
mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new
Run("Hello")))));
这已在工作,但发送的文本之间的间距仍然保持不变。
如何修复或更新我的richtextbox?我试图消除在显示richtextbox文本时的间距,如图所示
我想展示:
Hello
Hello
Hello
没有多个换行符或空格。文档不是字符串类型 编辑
string myText = new TextRange(richtextbox2.Document.ContentStart, richtextbox2.Document.ContentEnd).Text;
//replace two or more consecutive spaces with a single space, and
//replace two or more consecutive newlines with a single newline.
var str = Regex.Replace(myText, @"( |\r?\n)\1+", "$1", RegexOptions.Multiline);
那么我应该使用什么对象呢?我知道。文本在wpf c中不可用。要获取wpf中的文本,请参见:是的,它正在工作,但没有删除richtextbox中的间距。呵呵。如何删除I?你需要用新值更新richtextbox。你想这样显示“hellohellohello”或hello之前的空格吗?