Splitting up text
-
Upload
peter-andrews -
Category
Education
-
view
185 -
download
1
description
Transcript of Splitting up text
![Page 1: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/1.jpg)
SPLIT!
![Page 2: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/2.jpg)
Reading in other files
• VB can read in more than just text files• You can also read in CSV files• Comma Separated Value files• CSV is a way of storing data separated by
commas
![Page 3: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/3.jpg)
A CSV file opened in ExcelThe data one, two, three, four and five would be separated
by commas in a CSV file
![Page 4: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/4.jpg)
Using VB to read in a CSV fileDim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(" Y:\example.csv")
Console.WriteLine(fileReader)
Console.ReadLine() Notice the extension CSV
![Page 5: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/5.jpg)
MEANWHILE IN THE MASTER CHIEF’S KITCHEN . . . .
HOW AM I GONNA SPILT UP THAT CSV FILE!
![Page 6: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/6.jpg)
Splitting up CSV’s by CommaIdeally we want to spilt the CSV file up where the comma
exists
Splitting the file every time a
comma is found
![Page 7: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/7.jpg)
Splitting up CSV’s by Comma
Split(aString, ",")
We can use the split() function to split the CSV by commas
The name of the string variable we want to split up
Where we want the split to occur
![Page 8: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/8.jpg)
Splitting up CSV’s by CommaWe need something which is going to be able to hold these
separate values
![Page 9: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/9.jpg)
Splitting up CSV’s by Comma
Dim s As String Dim fileReader As String fileReader = My.Computer.FileSystem.ReadAllText("example.csv")
s = fileReaderDim fields As Array fields = Split(s, ",")
An Array will hold the split up CSV
![Page 10: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/10.jpg)
Splitting up CSV’s by CommaDim s As String Dim fileReader As String fileReader = My.Computer.FileSystem.ReadAllText(" Y:\example.csv") s = fileReaderDim fields As Array fields = Split(s, ",") For i = 0 To UBound(fields)Console.WriteLine("word " & i & " " & fields(i)) Console.WriteLine("*****************************************") Next
Using a for loop to write the contents of the array
![Page 11: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/11.jpg)
Splitting up CSV’s by Comma
![Page 12: Splitting up text](https://reader036.fdocuments.in/reader036/viewer/2022081414/54c04df64a7959c9128b461e/html5/thumbnails/12.jpg)
MEANWHILE IN THE MASTER CHIEF’S KITCHEN . . . .
PHEW!