Reading lines of a string variable

OK, so I know how to export a PowerShell string to a text file, and then read the text fine line by line to insert into a listbox.

I’d like to know if I can skip the text file all together, and just grab the result of the PowerShell command and read each line into the listbox.

I can capture the result of the PowerShell command just fine, but I’m stuck on how to read each line of this type of string.

Here is what I had when I was reading the text file. Is there a way to do this by just reading a string variable?

dim f2 as FolderItem = SpecialFolder.Temporary.child(“IUHAPPV”).child(“List.txt”)

dim lines As String
dim txtin as TextInputStream
txtin = TextInputStream.Open(f2)

while not txtin.EOF
lines=txtin.ReadLine
listbox1.AddRow(lines)
wend
txtin.close

Look at the Split function, and ReplaceLineEndings function. (If you are already certain about the line ending used, you can skip the latter in this case.)

Assuming you already know the line endings, and assuming they are linefeeds (Chr(10)), the code is simply this:

// ps has your string
dim lines() as string = ps.Split( Chr( 10 ) )
for i as integer = 0 to lines.Ubound
  ListBox1.AddRow( lines( i ) )
nexti

Awesome!

I did this to get rid of the extra line it was adding to the end.

dim lines() as string = theResult.Split( Chr( 10 ) ) for i as integer = 0 to lines.Ubound if lines(i) = "" then else ListBox1.AddRow( lines( i ) ) end next i

FYI, you could have also done this:

  theResult = theResult.Trim
  dim lines() as string = theResult.Split( Chr( 10 ) )
  for i as integer = 0 to lines.Ubound
    ListBox1.AddRow( lines( i ) )
  next i

However, if there might be blank lines within theResult and you want to skip them, your code above is better.

I just use

Listbox1.Cell(-1, -1) = theResult

which works quite nicely (on Windows anyway).