bob
bob

Reputation: 41

read chars from file to array vb.net

The file I need to read in is a square 8x8 or NxN. the chars are separated by spaces and at the end of the line I believe a crlf. When I try to read in the file i get the crlf's when i use this code:

Dim stream As New FileStream(fileNAME, FileMode.Open)
Dim reader As New StreamReader(stream)

Dim temparray() As String = reader.ReadToEnd.Split(" ")

I get stuff like this temparray(7) "K B"

what I need to do after that is put into a 2d array of 8x8 or NxN sample file is 8x8

or if there is a way to get it into a 2d array with out using a 1d array first that would be great.

sample file:

A B R A E L R K

B R E D A A O L

C A R R O T D I

H P N L K M I L

E P G A P P L E

E E O M N O K F

S L S R G A S A

E I F I S E H A

Upvotes: 1

Views: 1412

Answers (1)

brian
brian

Reputation: 180

String.Split can take an array of chars that you want to split on, so you can handle space, and new lines in one go.

Try:

Dim temparray() As String = reader.ReadToEnd.Split(New Char() {" "c, vbCr, vbCrLf})

In fact, it appears if you pass null/Nothing for the separater, Split will default to any whitespace characters. So this should work as well:

Dim temparray() As String = reader.ReadToEnd.Split(Nothing)

Upvotes: 2

Related Questions