0% found this document useful (0 votes)
11 views15 pages

18.12 - Files Split

The document explains the Split function, which separates strings into an array based on specified delimiters like commas and spaces. It demonstrates how to use Split with examples, including reading from files and storing values in arrays. Exercises are provided to practice reading lines and splitting information effectively.

Uploaded by

thikhe03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

18.12 - Files Split

The document explains the Split function, which separates strings into an array based on specified delimiters like commas and spaces. It demonstrates how to use Split with examples, including reading from files and storing values in arrays. Exercises are provided to practice reading lines and splitting information effectively.

Uploaded by

thikhe03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SPLIT (files)

View video
clips
 Split is a handy string function that allows you to
separate information.

 It will separate the information and place into an array


of type string

 This array is special – you don’t specify the size of it


Split()
Comma is the separator

string line = "C#,programming,is,fun"; //one line with commas

string[] array; //array declaration – no size given

array = line.Split(','); //split via comma and place in array

for (int i = 0; i < array.Length; i++)


Console.WriteLine(array[i]);

(0) (1) (2) (3)


C# programming is fun
Split()
Comma and space are separators

string line2 = "C# programming,is fun"; //line with commas and


// spaces

string[] array; //array declaration – no size given

array2 = line2.Split(',',' '); //split via comma and space


// and place into array

for (int i = 0; i < array2.Length; i++)


Console.WriteLine(array2[i]);

(0) (1) (2) (3)


C# programming is fun
Split
string line2 = "C# programming,is definitely not fun";

string[] array2; //array declaration – no size given

array2 = line2.Split(','); //split by comma and place into


// array

for (int i = 0; i < array2.Length; i++)


Console.WriteLine(array2[i]);

(0) (1)
C# programming is definitely not fun
 The next few exercises will read and split information. The
information is found in files.
 Previous reading from files did not need split. Why?

Values in file were on


different lines for Mandla.

Values are now on


the same line for Mandla,
so split is required.
Split() and Files
 Split allows for the separation of different information
when a line is read. ReadLine() reads the entire line.

pay.txt shows it separates


via spaces

StudentMarks2.txt
separates
via
commas
Exercise:
 The next exercise will load values into parallel arrays

 It will read the lines in the file and will use split since
values are separated by a space

 Split will create a one dim array and automatically place


Mandla into index 0, 10 into index 1,
15 into index 2 and 150 into index 3.
(0) (1) (2) (3)
Mandla 10 15 150
Split
 When using split, you need to declare a one dim array
of type string, for example

string[] array; // array used for split

 A size is not specified for the array since the size will
depend on the line to be split
array = myReader.ReadLine().Split(',');

// Explanation of line
Above code is used to read the entire line and split the line.

Values are placed into an array of type string.


Exercise

First time loop runs


array = myReader.ReadLine().Split(',');
(0) (1) (2)
Aggie 8 May 1979
Second time loop runs
array = myReader.ReadLine().Split(',');
(0) (1) (2)
Zinzi 9 April 1986
Third time loop runs
array = myReader.ReadLine().Split(',');
(0) (1) (2)
Hilda 17 June 1941
Exercise

First time loop runs


array = myReader.ReadLine().Split(',');
(0) (1) (2)
Aggie 8 May 1979

How to access individual elements from array?


array[0] gives you Aggie
array[1] gives you 8 May
array[2] gives you 1979
Exercise
int i, year, age;
string name,
message = "Names and Ages \n";
string[] array; //array for split
StreamReader reader = new StreamReader("Ages.txt");

for (i=1; i<=4; i++)


{
array = reader.ReadLine().Split(',');
// place array[0] into name variable
// place array[?] into year variable
// determine age (2019 minus …)
// add to message variable
}
MessageBox.Show(message, "Split");

(0) (1) (2)


Aggie 8 May 1979
Exercise
int i, year, age;
string name,
message = "Names and Ages \n";
string[] array; //array for split
StreamReader reader = new StreamReader("Ages.txt");

for (i=1; i<=4; i++)


{
array = reader.ReadLine().Split(',');
name = array[0];
year = int.Parse(array[2]);
age = 2019 - year;
message = message + name + " " + age
+ "\n";
}
MessageBox.Show(message, "Split");

(0) (1) (2)


Aggie 8 May 1979

You might also like