VB.net delimiter "","" split strings - vb.net

I'm using the below code to split a line into an array but some of the elements contain commas like the example below.
Current code : output.Add(sr.ReadLine().Split(New Char() {","c}))
Every element has "" around it in the CSV and I can't change it as it is from a supplier. I'm splitting the line into an array then removing the " using this.
'data(iRow)(0) = data(iRow)(0).Replace(Chr(34), "")
I'm running into problems because the elements like my,park that need to remain same.
Example Data : The data is read like this from the CSV/Text file
"my,park","mydate","my school","myhome"
My full function is below if that helps, been stuck on it for 2 days now.
Dim output As New ArrayList()
Using sr As StreamReader = New StreamReader(fileName)
While Not sr.EndOfStream
output.Add(sr.ReadLine().Split(New Char() {","c}))
End While
End Using
Return output
How can I complete this efficiently and get the correct output? The process is tripping up with the element containing commas and I don't know how to split on ","
Thanks in advance

Related

RichTextBox type of output & removing duplicates

I'm trying to make a small VB program to remove duplicate lines and empty lines from plain text.
I have the RichTextBox input but eventually I do not know what the type of the returned object is, is it an array or a list maybe ?
Also I'm trying to find the most efficient way to remove duplicate lines from big plain text(s), in Python I do it this way :
lines_nodupes = {}
for elt in lines :
lines_nodupes[elt] = ""
Since you can not have twice the same key, no duplicates are kept in the lines_nodupes dictionnary and I can enumerate it to access the lines.
There are two properties you can use :
Lines will return an array of Strings
Text will return the entire text as a string
See RichTextBox from MSDN
If you want to do as in Python :
Dim noDup as new Dictionary(Of String, String)
For Each line in MyRichTextBox.Lines
if not noDup.ContainsKey(line) then
noDUp.add(line, "")
End if
Next
You can also do (as suggested by VisualVincent in the coments) :
Dim noDup as new List(Of String)
For Each line in MyRichTextBox.Lines
if not noDup.Contains(line) then
noDUp.add(line)
End if
Next
which is a bit slower but difference won't be seen unless you have a very long list of items.

Removing blank fields from a delimited file using VB.NET

Using VB.NET (VS2013) I'd like to read a delimited file and remove a field if all records have the same blank field (could be multiple fields per record). The delimited file can have "x" number of columns and "y" number of rows and once the blank fields are removed, I need to write it back out as a new delimited file.
The input file will have a header that has to be maintained and the order of the records has to be maintained. I'm familiar with using TextParser to read the file and familiar with writing the file -- what I need help with is reading for a blank field and removing it if it exists across the entire file.
I was thinking I would have to use a datagrid but never used them so looking for some insight to point me in the direction.
Thanks!
I assume each line is delimited with a newline... open as a text file, read a line at a time and use the Split method to break up the line into an array of strings. The split method will take an argument that defines the delimiter you are using. Open the destination file and write the array of strings using a loop.
UPDATE
After thinking about it, you may have records that have a couple of blank columns and the rest have data. I'm not sure if you'd want to remove those columns, because you would lose the structure of the row if you did.
So instead of using StringSplitOptions.RemoveEmptyEntries, you can just do the Split() and then perform some Linq to find out if the Split() produced an array of empty strings.
Something like (with the same results)...
Imports System
Imports System.Linq
Public Module Module1
Public Sub Main()
Dim data As String = "|||||"
Dim pieces As String() = data.Split("|"c)
If pieces.Where(Function(p) String.IsNullOrEmpty(p) = False).Count = 0 Then
Console.WriteLine("All elements are empty")
Else
' Do something
End If
End Sub
End Module
Demo
OLD ANSWER
The String.Split() has an overload that excepts StringSplitOptions. Use StringSplitOptions.RemoveEmptyEntries with your String.Split() and if all records are blank then you'll end up with an array with a length of 0 that you can ignore.
Imports System
Public Module Module1
Public Sub Main()
Dim data As String = "|||||"
Dim pieces As String() = data.Split(new Char() {"|"}, StringSplitOptions.RemoveEmptyEntries)
If pieces.length = 0 Then
Console.WriteLine("All elements are empty")
Else
' Do something
End If
End Sub
End Module
Results:
All elements are empty
Demo

Reading from a CSV File in VB.Net where commas may exist in the data

I am reading data in from a text file. I know that each piece of data is separated by a comma and it looks like this in the text file:
"Requisition","Supplies Req GL.pdf","05/28/2014","8,200.00","0510","86107RC"
Here's where I am drawing a blank. The 4th piece of data on the line can contain commas, so when I do a split on the data, it also splits that piece as well.
How can I read this in, separate the data and also keep column 4 in tact.
If you know it's that field, then just join them together... a little hokey but it works:
Dim inLine As String()
Dim columns As New List(Of String)
Using sr as As New IO.StreamReader(args(0))
While Not sr.EndOfStream
inLine = sr.ReadLine.Trim().Split(CChar(","))
columns.Add(inLine(0))
columns.Add(inLine(1))
columns.Add(inLine(2))
If inLine.Length > 6 Then
columns.Add(inLine(3) & inLine(4))
columns.Add(inLine(5))
columns.Add(inLine(6))
Else
columns.Add(inLine(3))
columns.Add(inLine(4))
columns.Add(inLine(5))
End If
End While
End Using

Populating a combobox with specific items from a text file

I have a text file with records of machines stored on it, a single record looks like this:
"1234567890","12/04/2013","Saw","Hilti","17/11/2012",#TRUE#,#FALSE#,#FALSE#,"Made odd noise when operating"
What I would like to achieve is to populate a combo box with only the first item of each record - the serial number - so as to be able to search for the machine in a form based on the selected serial number. I need it to read the file, pick out the serial number from each record, fill the combo box with them, then allow you to select the one that you want.
Thanks in advance for any help.
if you are using a CSV formatted text file you can store info in a datatable and store each datarow in your cb, and use the DisplayMember property of the combobox to show only the first column. You also need that first line has the names of the columns to achieve this.
Try this supposing the first column is called Serial:
For Each r as DataRow in table.Rows
combobox1.Items.Add(r)
Next
combobox1.DisplayMember("Serial")
If not, you can still use your streamreader to obtain each line and split it to obtain first field.
Try:
Do
Dim line As String = reader.readLine()
If line Is Nothing Then Exit Do
Dim fields as String() = Split(line, ",")
combobox1.Items.Add(fields(0))
Loop
Hope it helps. You may have to use SubString method to retire the quotes. I post it apart:
replace
combobox1.Items.Add(fields(0))
by
combobox1.Items.Add(fields(0).SubString(1,fields(0).length -1))
If it's a simple app and you're happy to use the reader mentioned above, you can also do the following. I've used Replace() instead of Substring().
Dim reader As StreamReader = New StreamReader(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "yourFile.txt"))
Dim input = "", fields() As String
While (Not reader.EndOfStream)
input = reader.ReadLine()
fields = input.Split(",")
Dim itemToAdd = fields(0).Replace("""", "")
ComboBox.Items.Add(itemToAdd)
End While

Trim file after a blank line

I have a text file that has multiple blank lines and Im trying to return all the lines between two of them specifically
so if I have a file that looks like this:
____________________________
1########################
2##########################
3
4########################
5##########################
6#######################
7
8#########################
9##########################
10#######################
11####################
12########################
13#########################
14
15##########################
----------------------------
I would like to grab lines 8-13. Unfortunately, it might not always be 8-13 as it could be 9-20 or 7-8, but it will however always be between the 2nd and 3rd line break.
I know how to trim characters and pull out singular lines, but I have no idea how to trim entire sections.
Any help would be appreciated, even if you just point me to a tutorial.
Thanks in advance.
The basic idea here is to get the entire thing as a string, split it into groups at the double line breaks, and then reference the group you want (in your case, the third one).
Dim value As String = File.ReadAllText("C:\test.txt")
Dim breakString As String = Environment.NewLine & Environment.NewLine
Dim groups As String() = value.Split({breakString}, StringSplitOptions.None)
Dim desiredString As String = groups(2)
MsgBox(desiredString)
Edit:
In response to the question in your comment -
Environment.NewLine is a more dynamic way of specifying a line break. Assuming you're running on windows - you could use VbCrLf as well. The idea is that if you were to compile the same code on Linux, it Environment.NewLine would generate a Lf instead. You can see here for more information: http://en.wikipedia.org/wiki/Newline
The reason I used Environment.NewLine & Environment.NewLine is because you want to break your information where there are two line breaks (one at the end of the last line of a paragraph, and one for the blank line before the next paragraph)
What I ended up doing was trimming the last part and searching for what I needed in the first part (I know I didnt include the searching part in the question, but I was just trying to figure out a way to narrow down the search results as it would have had repeated results). Im posting this incase anyone else stumbles upon this looking for some answers.
Dim applist() = System.IO.File.ReadAllLines("C:\applist.txt")
Dim findICSName As String = "pid"
Dim ICSName As New Regex("\:.*?\(")
Dim x = 0
Do Until applist(x).Contains("Total PSS by OOM adjustment:")
If applist(x).Contains(findICSName) Then
app = ICSName.Match(applist(x)).Value
app = app.TrimStart(CChar(": "))
app = app.TrimEnd(CChar("("))
ListBox1.Items.Add(app)
End If
x = x + 1
Loop
End If
How this works is that it looks through each line for the regex until it reaches first word in the breakpoint "Total PSS by OOM adjustment:"