Insert carriage return on nth line in textarea box

Hi,
Needed help with textarea.
I have a multiple line of text in a textarea box like the following

I have one.
I have two.
I have three.
I have four.
I have five.
… and so on.

Question is how can do the following

  1. I insert a new line or carriage on EVERY nth line.
  2. Insert a repetitive character to each divided set

Desired effect:
1 I have one.
2 I have two.
3 I have three.

1 I have four.
2 I have five.
3 I have six.

1 I have seven.
… and so on.

Greatly appreciated. Thank you.

something like this?

v=split(textArea1.text,endofline)
for i=0 to v.ubound step N 
v(i)=v(i)+endofline
next i
textarea1.text=join(v,endofline)

Step is one of the most often forgotten aspects of “For … Next”. I depended on it for much back in MSBASIC/GWBASIC and until Dave’s example I’d forgotten about it and have been doing idiotic things like:

For x = 0 To My Count // Skip to every third ProgressBar.Refresh x = x + 3 Next

Sorry, can anyone kindly show an example using Dave’s solution?

How does my solution not stand on its own merit?

what portion of the process do you not understand?

v=split(textArea1.text,endofline) // take the text in the TextArea and split it into individual lines
for i=0 to v.ubound step N // iterate over that set of lines every "N" lines
v(i)=v(i)+endofline // Add an additional endofline to every Nth line
next i // end of loop
textarea1.text=join(v,endofline) // join the array back into the textarea by endofline

Thank you very much.
I did not manage to cause a “break” after the third line.
Which have me thinking, maybe what I need is a new line insert instead of a “EndOfLine “
Is there really any difference?

Maybe you should add the correct EndOfLine Type to the EndOfLine.

v(i)=v(i)+EndOfLine.Windows

See http://documentation.xojo.com/api/code_execution/end.htmlOfLine for more information.

Based on Dave’s code, you might need to tweak the EndOfLine.Macintosh/EndOfLine.Windows depending on your platform.

Dim v() As String v = Split(textArea1.Text, EndOfLine.Macintosh) For i As Integer = 0 To v.ubound v(i) = Str((i Mod 3) + 1) + " " + v(i) + If(i Mod 3 = 2, EndOfLine.Windows, "") Next i textarea1.Text = Join(v, EndOfLine.Windows)

or like this if you dont like inline ifs

Dim v() As String v = Split(textArea1.Text, EndOfLine.Macintosh) For i As Integer = 0 To v.ubound v(i) = Str((i Mod 3) + 1) + " " + v(i) If i Mod 3 = 2 Then v(i) = v(i) + EndOfLine.Windows End If Next i textarea1.Text = Join(v, EndOfLine.Windows)

Paul Sondervan, Dave S, Julian S solution solved the problem.

I like to thank everyone who answered my call.

One last question:

Julian S solution cleanly break it down into sets of three and adding number to each line.

How can I modify so that it breaks down into sets of nth number of line and adding number to each line?

Dim v() As String Const n As Integer = 3 'Change to a Dim not Const if you want to alter this at runtime v = Split(textArea1.Text, EndOfLine.Macintosh) For i As Integer = 0 To v.ubound v(i) = Str((i Mod n) + 1) + " " + v(i) + If(i Mod n = n - 1, EndOfLine.Windows, "") Next i textarea1.Text = Join(v, EndOfLine.Windows)

Thank you very much!!!