vb.net - read the last line of multi-line string separated by line feeds -
i have variable held in memory string several lines. name of variable 'trace2'
i.e.
dim trace2 string
instead of reading every line want read last line.
all can find examples of reading text file, not variable held in memory
tia
added
i tried this, dont because dont want create file
dim filenameandpath string = "0trace2.txt" dim filecontent string dim filelines() string using sr new system.io.streamreader(filenameandpath) filecontent = sr.readtoend filelines = filecontent.split(system.environment.newline.tochararray) if filelines.count >= 2 linenumber integer = filelines.getupperbound(0) - 1 filelines.getupperbound(0) msgbox(filelines) next end if sr.close() end using
why not file.readlines
, enumerable.last
?
dim lastline string = file.readlines(filenameandpath).lastordefault() ' nothing if file empty
file.readlines
not need read lines before can start processsing, it's cheap in terms of memory consumption.
if have string
:
dim lastline = trace2.split({environment.newline}, stringsplitoptions.none).lastordefault()
Comments
Post a Comment