split by crlf using VB.net - 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)

Related

Data download from PHP is not split into newlines

I am retrieving data from web. Data is seperated by each line. Data looks like this
Data1
Data2
Data3
I want to alert for each data found on the webpage. Tried this,
Dim Lines() As String
Dim stringSeparators() As String = {vbCrLf}
Dim Source As String
Dim wc As New WebClient
Source = wc.DownloadString("http://www.example.com/data.php")
Lines = Source.Split(stringSeparators, StringSplitOptions.None)
For Each s As String In Lines
MsgBox(s)
Next
But unfortunately, it alerts once all the data. My question is, how to alert for each data ?
vbCrLf, as defined in Constants, won't match a single UNIX-style newline - "Newline" (\n), LF/LINEFEED, ASCII 10 - character as transmitted from PHP.
To deal with both Windows and UNIX/Linux end-of-line sequences, use:
Dim stringSeparators() As String = {vbLf, vbCrLf}
The order the separators supplied does not matter, see the remarks in String.Split for details.
While the above solves the problem in a fairly robust manner, it may better to use the exact EOL format, especially when writing - and to make a selection prior based on established format. In this case that might be only using vbLf which would work for the given PHP output, but would incorrectly leave in CR characters for Windows text files.
When dealing with system-native text files, or Windows components such as Controls, vbNewLine should generally be preferred over vbCrLf: vbCrlLf is appropriate when the goal is to be explicit, as above, and only accept/emit a specific ASCII sequence as mandated by protocols and conventions.
When dealing with whitespace characters, I often end up running the String.Asc() method on them, to see what they really are.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.asc(v=vs.110).aspx
Then, you can ensure that you are splitting on the correct character.

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))

Can't split string with space character using Split(" "c) 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)

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: ""