How can I get the last line of textarea?

How can I get the last string of textarea?

I think I misunderstand about ‘SelStart…’.
Below code shows every strings.


  dim lastLine as integer = ResultBufferTextArea.LineNumAtCharPos(ResultBufferTextArea.Text.Len())
  
  ResultBufferTextArea.SelStart = lastLine
  ResultBufferTextArea.SelLength =ResultBufferTextArea.Text.Len - ResultBufferTextArea.SelStart
 
  MsgBox "Seltext: " + ResultBufferTextArea.SelText

If by “last string” you mean the last word, try this:

Dim entireText as string=ResultBufferTextArea.Text
dim words() as string=split(entireText," ")
dim lastWord as string=words(ubound(words))

Thank you.

Tried to run a Timer to print the ‘lastWord’ string, but strangely it prints every strings.
The textarea data is appended by AppendText.

Something wrong?

DataAvailable event(Shell)

  Dim ResultBuffer As String
  ResultBuffer = Me.ReadAll
  
  ResultBufferTextArea.AppendText  ResultBuffer

Timer


  Dim entireText as string=ResultBufferTextArea.Text
  dim words() as string=split(entireText," ")
  dim lastWord as string=words(ubound(words))
  
  MsgBox "lastWord: " +lastWord

PS. What about the last line?

Hmm.
Try:

dim Lines() as string=split(entireText,EndOfLine)
dim lastLine as string= Lines(ubound(Lines))

That should give you the last line.

I’d add a replacelineendings as well just to be safe

dim Lines() as string=split( replaceLineEndings(entireText, EndOfLine) , EndOfLine)
dim lastLine as string= Lines(ubound(Lines))

Regular expressions, people!

Assuming you want the last line of content excluding trailing white space:

dim rx as new RegEx
rx.SeachPattern = "^(?=[^\\r\
]*\\S).+(?=\\s*\\Z)"

dim lastLine as string
dim match as RegExMatch = rx.Search( entireText )
if match isa RegExMatch then
  lastLine = match.SubExpressionString( 0 )
end if

“^(?=[^\r
]\S).+(?=\s\Z)”

Is that Regex or Klingon? :stuck_out_tongue:

Don’t use the timer use the event complete

I like something readable :stuck_out_tongue:

[quote=262905:@brian franco]"^(?=[^\r
]\S).+(?=\s\Z)"

Is that Regex or Klingon? :P[/quote]

KemTalk :stuck_out_tongue:

This is the code equivalent to that regular expression:

dim lastLine as string
dim lines() as string = ReplaceLineEndings( entireText, &uA ).Split( &uA )
for i as integer = lines.Ubound downto 0
  dim l as string = lines( i )
  if l.Trim <> "" then
    lastLine = l
    exit for i
  end if
next i

Curious why you’d use &uA since its not native to all platforms & any subsequent use would require replacing &uA with “endofline”
And its less obvious to the average reader that &uA IS “end of line”
So … why ?

and fwiw it wasn’t “last non-empty line” - just “last line” which your regex isn’t doing

The point is just to make sure Split works there, and that’s how I would have written it. EndOfLine or &uD would have worked just as well.

Neither the code nor the regex may be what the OP wanted, but I mentioned my assumption in my first post. I was just illustrating the equivalent code to the pattern as posted to demonstrate the contrast and why regular expressions can be helpful.

like I said - just curious why you’d use &uA

Maximilian, Norman:
Sorry to say that I don’t see any string with the new code.

Loannis:
Through the shell, I run a Jar file and receive the output of standard out. It is the number of the processed rows.
Since I should print the processed number by 5 seconds, I can’t use Complete event and furthermore I can’t use DataAvailable event either.
That is why I keep a text area to save the received output and parse the file with a Timer.Action.
Please let me know if I am wrong.

Kem:
Regular expression version of your post works well.
Thank you so much.

I learned how to use a regular expression and ReplaceLineEndings to parse the specific strings here.
Thank you everyone!

From that you say is like the shell command have endless loop.
The Complete event Triggered whenever the executed command is finished or the Close method is called.
like this

dim s as shell dim mc as string mc ="ls" s.execute (ls)
when the “ls -h” have the list completed the the complete event triggered.

Yes, I see. My shell command would last around 1 hour.

I realized that I should not use a textarea as I should run several jobs in parallel and the number of it is not fixed.
So, like you said, I believe I I should use DataAvailable event to catch the information of the processed number.

Fortunately, DataAvailable event will be triggered every 10 seconds, therefore I don’t need to use another Timer.

Thanks for your idea.