RichTextBox Multiline to RichTextBox Singleline
Hi all :wave:, I have a small problem, I'm loading a file to RichTextBox1:
Quote:
Number: 1' Location: DVD' NrLocation: 1' Name: Movie 1' Year: 2013' Number: 2' Location: DVD' NrLocation: 2' Name: Movie 2' Year: 2013' Number: 3' Location: DVD' NrLocation: 3' Name: Movie 3' Year: 2013' Number: 4' Location: DVD' NrLocation: 4' Name: Movie 4' Year: 2013' Number: 5' Location: DVD' NrLocation: 5' Name: Movie 5' Year: 2013' Number: 6' Location: DVD' NrLocation: 6' Name: Movie 6' Year: 2013' Number: 7' Location: DVD' NrLocation: 7' Name: Movie 7' Year: 2013' Number: 8' Location: DVD' NrLocation: 8' Name: Movie 8' Year: 2013' Number: 9' Location: DVD' NrLocation: 9' Name: Movie 9' Year: 2013' Number: 10' Location: DVD' NrLocation: 10' Name: Movie 10' Year: 2013' Number: 11' Location: DVD' NrLocation: 11' Name: Movie 11' Year: 2013' Number: 12' Location: DVD' NrLocation: 12' Name: Movie 12'
I want to copy all numbers to RichTextBox2 and all names to RichTextBox3, so, my RichTextBox2 should look like this:
and my RichTextBox3 should look like this:
Code:
Movie 1
Movie 2
Movie 3
...
so far I got only the first number and the first name, but I need all of the numbers and all names. :(
Re: RichTextBox Multiline to RichTextBox Singleline
Ah yes, Movie 2, always loved that one!
Split on '[space] and loop through the resulting array checking for Number or Name would appear to be the obvious solution.
Re: RichTextBox Multiline to RichTextBox Singleline
Quote:
Originally Posted by
dunfiddlin
Ah yes, Movie 2, always loved that one!
Split on '[space] and loop through the resulting array checking for Number or Name would appear to be the obvious solution.
Do you have any examples? :(
I even found this but still nothing:
Code:
Dim lines() As String = RichTextBox1.Lines
RichTextBox2.Text = Array.Find(lines, Function(s) s.StartsWith("Number: ")).Split(New String() {":"}, StringSplitOptions.None)(1).TrimEnd("'"c)
RichTextBox3.Text = Array.Find(lines, Function(s) s.StartsWith("Name: ")).Split(New String() {":"}, StringSplitOptions.None)(1).TrimEnd("'"c)
Re: RichTextBox Multiline to RichTextBox Singleline
vb.net Code:
Dim s As String = "Number: 1' Location: DVD' NrLocation: 1' Name: Movie 1' Year: 2013' Number: 2' Location: DVD' NrLocation: 2' Name: Movie 2' Year: 2013' Number: 3' Location: DVD' NrLocation: 3' Name: Movie 3' Year: 2013' Number: 4' Location: DVD' NrLocation: 4' Name: Movie 4' Year: 2013' Number: 5' Location: DVD' NrLocation: 5' Name: Movie 5' Year: 2013' Number: 6' Location: DVD' NrLocation: 6' Name: Movie 6' Year: 2013' Number: 7' Location: DVD' NrLocation: 7' Name: Movie 7' Year: 2013' Number: 8' Location: DVD' NrLocation: 8' Name: Movie 8' Year: 2013' Number: 9' Location: DVD' NrLocation: 9' Name: Movie 9' Year: 2013' Number: 10' Location: DVD' NrLocation: 10' Name: Movie 10' Year: 2013' Number: 11' Location: DVD' NrLocation: 11' Name: Movie 11' Year: 2013' Number: 12' Location: DVD' NrLocation: 12' Name: Movie 12'"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim splits = s.Split({"' "}, StringSplitOptions.RemoveEmptyEntries)
For Each st In splits
If st.StartsWith("Number:") Then
RichTextBox2.AppendText(st.Split(" "c)(1) & vbCrLf)
ElseIf st.StartsWith("Name:") Then
RichTextBox3.AppendText(st.Split({": "}, StringSplitOptions.None)(1) & vbCrLf)
End If
Next
End Sub
Re: RichTextBox Multiline to RichTextBox Singleline
Another way using regular expressions.
vb Code:
Imports System.Text.RegularExpressions
Public Class MainForm
Private ReadOnly m_source As String = "Number: 1' Location: DVD' NrLocation: 1' Name: Movie 1' Year: 2013'" & _
"Number: 2' Location: DVD' NrLocation: 2' Name: Movie 2' Year: 2013'" & _
"Number: 3' Location: DVD' NrLocation: 3' Name: Movie 3' Year: 2013' " & _
"Number: 4' Location: DVD' NrLocation: 4' Name: Movie 4' Year: 2013' " & _
"Number: 5' Location: DVD' NrLocation: 5' Name: Movie 5' Year: 2013' " & _
"Number: 6' Location: DVD' NrLocation: 6' Name: Movie 6' Year: 2013' " & _
"Number: 7' Location: DVD' NrLocation: 7' Name: Movie 7' Year: 2013' " & _
"Number: 8' Location: DVD' NrLocation: 8' Name: Movie 8' Year: 2013' " & _
"Number: 9' Location: DVD' NrLocation: 9' Name: Movie 9' Year: 2013' " & _
"Number: 10' Location: DVD' NrLocation: 10' Name: Movie 10' Year: 2013' " & _
"Number: 11' Location: DVD' NrLocation: 11' Name: Movie 11' Year: 2013' " & _
"Number: 12' Location: DVD' NrLocation: 12' Name: Movie 12' Year: 2013'"
Private ReadOnly m_pattern As String = "Number\: (?<Number>.+?)\' Location\: DVD\' " & _
"NrLocation: \d{1,}' Name\: (?<Name>.+?)\' Year\: \d{4}\'"
Private ReadOnly m_match As New Regex(m_pattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim matches = Me.m_match.Matches(Me.m_source)
For Each m As Match In matches
Me.RichTextBox1.AppendText(m.Groups("Number").Value & Environment.NewLine)
Me.RichTextBox2.AppendText(m.Groups("Name").Value & Environment.NewLine)
Next
End Sub
End Class
Re: RichTextBox Multiline to RichTextBox Singleline
Thanks guys, you are the best. :) Best forum ever. :wave: