Insert carriage return on nth line in textarea box

  1. 6 weeks ago

    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

    Feb 12 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

    Feb 12 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. Sorry, can anyone kindly show an example using Dave's solution?

  5. Dave S

    Feb 12 San Diego, California USA
    Edited 6 weeks 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. 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

    Feb 13 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

    Feb 13 Pre-Release Testers, Xojo Pro UK
    Edited 6 weeks 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. Paul Sondervan, Dave Sisemore, Julian S solution solved the problem.

    I like to thank everyone who answered my call.

  10. 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

    Feb 13 Pre-Release Testers, Xojo Pro UK
    Edited 6 weeks 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. Thank you very much!!!!!

or Sign Up to reply!