remove blank strings in arraylist

J

James

below is some codes.

my arraylist below reads from a file. My files contains blank line (ie
carriage return)

My message dialog shows all strings being captured. However i do not want my
array to contain "blank" string/line/carriage return

How do i remove the index so that my arraylist become smaller ?





****************************

Dim sr As StreamReader = New StreamReader(filespath)

Dim line As String

Do

line = sr.ReadLine

'add each line into arraylist

If InStr(line, ";") <> 1 Then

filestoexecute.Add(line)

End If

Loop Until line Is Nothing

sr.Close()

*****************************

For Each i In filestoexecute

MessageBox.Show(i)

Next

****************************
 
P

Phill W.

my arraylist below reads from a file. My files contains blank line (ie
carriage return) .. . .
How do i remove the index so that my arraylist become smaller ?

So you're trying to leave out blank lines and comments?
If InStr(line, ";") <> 1 Then

If the line is blank, Instr() will return 0, which isn't 1, so will add the
line.
To exclude blank lines as well (if I can remember the "old" syntax for
this),
try this :

If Len( line ) > 0 _
AndAlso Instr( line, ";" ) <> 1 _
Then

(BTW, using the methods on the String class, thios would be)

If line.Length > 0 _
AndAlso Not line.StartsWith( ";" ) _
Then

HTH,
Phill W.
 
H

Herfried K. Wagner [MVP]

James said:
my arraylist below reads from a file. My files contains blank line (ie
carriage return)

My message dialog shows all strings being captured. However i do not want
my array to contain "blank" string/line/carriage return

How do i remove the index so that my arraylist become smaller ?
[...]
Do

line = sr.ReadLine

'add each line into arraylist

If InStr(line, ";") <> 1 Then

\\\
If Len(line) > 0 AndAlso InStr(line, ";") <> 1 Then
...
End If
///

If you want to check the string with spaces on the right and left end of the
string, use 'Len(Trim(line)) > 0'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top