vb.net 如何阅读 RichTextBox 中不同行的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13402420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read Different lines of text in a RichTextBox
提问by user1713546
I want to know how to read and convert the different lines of a richtextbox in vb.net for example if these are the lines of a RichTextBox:
我想知道如何在 vb.net 中读取和转换 RichTextBox 的不同行,例如,如果这些是 RichTextBox 的行:
Hello
Hi
How can I convert it to something like:
我怎样才能把它转换成类似的东西:
Yo(Hello)
Yo(Hi)
and put the result in a second richtextbox?
并将结果放入第二个富文本框?
回答by igrimpe
RichtextBox has a linesproperty:
RichtextBox 有一个lines属性:
Dim rtb_in As New RichTextBox
Dim rtb_out As New RichTextBox
For Each line In rtb_in.Lines
rtb_out.AppendText(String.Format("Foo({0})", line))
Next
Always a good idea to check out MSDN for the classes you work with ...
查看 MSDN 以了解您使用的课程总是一个好主意......
回答by NeverHopeless
Possibly you should use String.Jointo accomplish it. A one linesolution would be:
可能你应该用String.Join它来完成它。一个one line解决方案是:
rtbOut.Lines = ("Yo(" & String.Join(")" & Environment.NewLine & "Yo(", rtbIN.Lines) & ")").Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
and here is the complete code where modified line is assigned to second RichTextBox:
这是将修改后的行分配给第二个的完整代码RichTextBox:
Dim rtbIN As New RichTextBox
Dim rtbOut As New RichTextBox
rtbIN.Lines = New String() {"Hello ", "Hi"}
rtbOut.Lines = ("Yo(" & String.Join(")" & Environment.NewLine & "Yo(", rtbIN.Lines) & ")").Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
回答by Sean Airey
You could try splitting on a new line and modifying the results:
您可以尝试在新行上拆分并修改结果:
Dim box1Lines as String() = richTextBox1.Text.Split(new String() { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
Dim newLines as String = ""
For Each line As String in box1Lines
newLines += "Yo(" & line & ")" & Environment.NewLine
Next
richTextBox2.Text = newLines

