Can't split string with space character using Split(" "c) vb.net - vb.net

I'm trying to split a Yahoo historical stock price csv file, downloaded into a string, by what looks like a space character. I want a new row for each split. The split function works for other characters I see in the string. I suspect the characters may be a non breaking space character but I've been unable to split on them.
This is the test csv file that is downloaded into the string:
http://ichart.finance.yahoo.com/table.csv?s=AAPL&c=2011
I'm trying to split the string like this:
Dim rows As String() = data.Split(" "c)
There is a real space character in the header part of the string that this breaks on, but not the white space characters in the stock data I want to split. If this is a non breaking space, how do I split on it? How can I tell what this white space character is?
A Sample of the string looks like this:
"Date,Open,High,Low,Close,Volume,Adj Close 2011-12-27,69.24,72.18,69.01,71.55,1491000,71.55 2011-12-23,67.49,69.25,67.25,69.08,880300,69.08"
I'm trying to split at the space before the stock dates, "2011-12-23", for example.
This is my function:
Public Shared Function DownloadData(ByVal ticker As String, ByVal yearToStartFrom As Integer) As List(Of HistoricalStock)
Dim retval As New List(Of HistoricalStock)()
Using web As New WebClient()
Dim data As String = web.DownloadString(String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&c={1}", ticker, yearToStartFrom))
Dim rows As String() = data.Split(" "c)
Return retval
End Using
End Function

Those "spaces" that you talk about aren't really spaces, they are line returns. You probably opened it with NotePad, where it's displayed as a space, because it tries to open it with the wrong encoding, I suppose.
Open it with WordPad or Excel and you'll see the line returns. You will need to split on vbLf for it to work:
Dim rows As String() = data.Split(vbLf)

Those are not spaces. They are line feeds. This will correct it.
Dim rows As String() = data.Split(vbLf)

Related

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

Visual Basic removing remaing string after delimiter

I'm trying to remove the last part of the string to make it more presentable. The string looks like this
Stringname.number.RemainingStringTobeRemoved
Is there a way to remove the last part without using string.Substring(0,string.Length-10)?
I want it to be dynamic, the string I'll be removing is not a constant number.
My initial idea was to use delimiter to identify the starting point of the part I want to remove.
My initial idea was using delimiter to identify the starting point of the part I want to remove.
That's a good idea if your string is always the same format and the substring you want to remove does not contain that delimiter. Take a look at LastIndexOf.
Spoiler:
Dim s = "Stringname.number.RemainingStringTobeRemoved"
Dim r = s.Substring(0, s.LastIndexOf("."))
You can use Split to achieve this
String s = "Stringname.number.RemainingStringTobeRemoved"
Dim words As String() = s.Split(New Char() {"."c})
String RequiredString = words(0) & words(1)
The RequiredString is what you need
Using a delimiter is one way. You can use the Split method to split the string into parts at the ..
Dim FullString As String = "Stringname.number.RemainingStringTobeRemoved"
Dim StringParts() As String = Strings.Split(FullString, ".", 3)
Return StringParts(0) & StringParts(1)
The Split method returns an array of String that contains the parts. The array will contain the following items
Stringname
number
RemainingStringTobeRemoved
The delimiters are omitted.

Isolate a a substring within quotes from an entire line

To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?
Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))

split by crlf using VB.net

Need help how to proper split a string by crlf
below is the code:
Dim str As String = "Hello" & vbCrLf & "World"
Dim parts As String() = str.Split(ControlChars.CrLf.ToCharArray)
For Each part As String In parts
MsgBox(part)
Next
Output
Hello
World
I want to get rid the blank space in between the two.
Hello
World
If you want to support all kinds of newlines (CR, LF, CR LF) and don’t need blank lines, you can split on both characters and make use of the String.Split option that stops it from producing empty entries:
str.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
(This also produces an empty array if the input is empty.)
If you know you have consistent CR LF newlines and you want to preserve blank lines, you can split on the entire delimiter:
str.Split({ControlChars.CrLf}, StringSplitOptions.None)
The given answer splits on any cr OR lf and removes blanks; that works ok for the given case, but it would remove 'real' empty lines as well (and feels unclean to me).
Alternative:
System.Text.RegularExpressions.Regex.Split(str, vbCrLf)
(note that the second string is a regex-pattern, special chars would have to be escaped)
This code will split on line-feed, new line or both(vbCrLf). It will not remove empty lines.
Dim arr() As String = System.Text.RegularExpressions.Regex.Split(text, "(\r\n|\r|\n)", _
RegularExpressions.RegexOptions.ExplicitCapture)

VB.NET split string with quotation marks in it

Trying to split a line wherever "," appears (with the quotation marks). The problem is VB.NET uses " to start/end strings, so I tried using .Split(""",""") but that then splits it by " not ",".
Try something like this:
Dim TestToSplit As String = "Foo"",""Bar"
Dim Splitted() As String = TestToSplit.Split(New String() {""","""}, StringSplitOptions.None)
I just tested it and got an array with Foo And Bar. I hope this helps.
The Split function (the way you are using it) expects a Char. If you want to split on multiple characters you need to use a string array. (Seems to me another overload of a single string value would have been handy.)
This function splits a line of text and returns an array based on the delimiter you have specified. (Of course, you could make this more general purpose by passing in the separator array.)
Private Function SplitLine(ByVal lineOfText As String) As String()
Dim separator() As String = {""","""}
Dim result() As String
result = lineOfText.Split(separator, StringSplitOptions.None)
Return result
End Function
Another alternative I often find useful is this:
Regex.Split(textToSplit, """,""")
Lets you split on more complex criteria than an array of alternative separators.
To escape the "-character in VB.NET, use two: ""