385 views

# Insert carriage return on nth line in textarea box

1. 8 months ago

### Benny L

is not verified 12 Feb 2019

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.

2. ### Dave S

12 Feb 2019 San Diego, California USA

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)```
3. ### Tim J

12 Feb 2019 Pre-Release Testers, Xojo Pro Dehydrating in AZ

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```
4. ### Benny L

is not verified 12 Feb 2019

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

5. ### Dave S

12 Feb 2019 San Diego, California USA
Edited 8 months ago

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```
6. ### Benny L

is not verified 13 Feb 2019

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?

7. ### Paul S

13 Feb 2019 Pre-Release Testers, Xojo Pro Europe (Netherlands, Den Haag)

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

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

See http://docs.xojo.com/EndOfLine for more information.

8. ### Julian S

13 Feb 2019 Pre-Release Testers, Xojo Pro UK
Edited 8 months ago

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)```
9. ### Benny L

is not verified 13 Feb 2019

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

I like to thank everyone who answered my call.

10. ### Benny L

is not verified 13 Feb 2019

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?

11. ### Julian S

13 Feb 2019 Pre-Release Testers, Xojo Pro UK
Edited 8 months ago
```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)```
12. ### Benny L

is not verified 13 Feb 2019

Thank you very much!!!!!

or Sign Up to reply!