This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We'll read the CSV file line by line.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's get our soccer game data
into a more workable format.
0:00
We can try to break up this
big file content string
0:03
by using some helpful
methods on the string class.
0:07
Our string actually has some
hidden characters in it.
0:10
In a text document like data.text,
0:13
when I press the Enter key,
the cursor moves to the next line.
0:16
Well, that's actually being stored as
a new line, or a line break character.
0:21
Another word for it is a carriage return,
which is, kind of,
0:26
a holdover from the typewriter days when
the carriage physically moved down and
0:29
to the beginning of the next line,
like it does in our text document.
0:34
In some systems, it's only one character,
and in others, it's two characters.
0:38
And those characters can differ.
0:43
In Unix,
it's called a line feed character.
0:45
In other systems,
it's called a carriage return.
0:47
And in Windows, it's two characters,
both a line feed and a carriage return.
0:50
Confusing, right?
0:55
We can use these characters
to our advantage,
0:56
though, in combination with a method
on the String class called Split.
0:58
Let's check out the documentation
on String.Split.
1:03
String.split.net.
1:07
Here it is.
1:12
Returns a string array that contains
the substrings in this instance that
1:15
are delimited by elements of a specified
string or Unicode character array.
1:20
Notice the word delimited.
1:25
Delimited means that
something has a boundary.
1:28
So in the case of our CSV file,
each of the lines in the file
1:30
are delimited by a newline
character at the end.
1:35
Let's try and figure out what
those characters look like.
1:38
We can set a breakpoint in Visual Studio,
and
1:41
take a peek at what the value
of fileContents looks like.
1:43
Press F5 to debug.
1:48
We can hover over the file
contents here and see its value.
1:51
It's pretty long, but I can see some funny
stuff right here, with the backslashes.
1:56
We've got a \r, \n,
those must be our newline characters.
2:01
These backslashes are indicating
escape sequences.
2:06
Like the \u in our Unicode character,
and the \\ in our string directory.
2:10
The \r, \n, represents two
escape sequences in this string.
2:16
The \r is a carriage return,
and the \n is a newline.
2:21
There are a handful of other escape
sequences too, for quotes, and tabs, and
2:27
other characters.
2:30
I've linked to the full list in the notes.
2:31
The backslash itself has to be
represented by an escape sequence,
2:34
the double backslash, since it's used to
indicate an escape sequence has started.
2:38
We can stop debugging, and
let's use the Split method
2:44
on the file content string,
fileContents.Split().
2:49
It'll return a string array, so
let's assign it to a string[] fileLines,
2:54
And then, we'll pass in an array
of our newline characters.
3:03
(new char[ ] {, and so
we'll do a '\r' character,
3:06
then we'll also do a '\n'.
3:14
Then, we can print each one
of these to the console.
3:21
Foreach(var line in fileLines).
3:26
Console.WriteLine, and (line).
3:35
Let's run this and see what we get.
3:44
Ctrl+F5.
3:47
Well it looks like we've got
a double line break here.
3:52
I think since we're passing it two
characters, it's splitting our string at
3:55
the carriage return, and the newline, and
giving us an empty string between the two.
3:59
That's not really ideal.
4:04
Well, we could go in afterwards,
4:06
and try to remove or
ignore the empty string elements later.
4:07
But let's check out the documentation
on String.Split again.
4:11
One of these overloads might help us.
4:15
Aha, splits a string into substrings
based on the characters in an array.
4:18
You can specify whether the substrings
include empty array elements.
4:23
We can use this overload to tell it that
we don't want the empty array elements.
4:28
I bet a lot of developers
had the same problem we did.
4:32
Back in our code, we'll pass an additional
4:35
parameter here, StringSplitOptions,
4:41
and then, .RemoveEmptyEntries.
4:47
This StringSplitOptions is a type
you may not have seen before.
4:53
It's called an enum,
which is short for a enumeration.
4:58
An enum is a type that defines
a related set of constants.
5:01
Let's check it out, Go To Definition.
5:05
So this enum has two constants defined,
None and RemoveEmptyEntries.
5:10
Each of these constants has
an integer value attached to it.
5:16
Enums are pretty useful for when you have
a finite set of values, like Options.
5:20
We'll get into more enums
later on in this course.
5:25
Let's run it to see if our
empty array elements are gone.
5:29
Ctrl+F5.
5:33
Looks like it worked.
5:35
Now that we could read our file,
next up is to do something with it.
5:38
In the videos that follow,
5:43
we'll parse our data into something
we can use in our application.
5:44
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up