console write line every 8 characters - vb.net

I'm converting text to binary, and sending that information to the console. However, I want the console to write a new line for every 8 characters.
My convert to binary code looks like this (where Result.Text contains random text, that will be converted)
Dim Resultconvert As String = ""
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Resultconvert &= s
Next
Console.WriteLine(Resultconvert)
So it should look like this:
01110010
01111110
01101111
01011100
01100100
01010001
01001101
00111010
01010100
instead of:
011100100111111001101111010111000110010001010001010011010011101001010100
Any help is appreciated. Thank you in advance.

Dim Resultconvert As String = String.Empty
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Debug.Print(s)
Resultconvert &= s
Next

The simplest solution is to just call WriteLine separately for each byte, like this:
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Console.WriteLine(s)
Next
However, if you want to write it all at once, you need to append a new line to the string after each byte, like this:
Dim Resultconvert As String = ""
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Resultconvert &= s & Environment.NewLine
Next
Console.WriteLine(Resultconvert)
But, at that point, it would be easier and more efficient to use a StringBuilder:
Dim builder As New StringBuilder()
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
builder.AppendLine(s)
Next
Console.WriteLine(builder.ToString())

Related

Combining two string and insert element VB.Net

I tried to build a combination algorithm between 2 strings, unfortunately it has some errors.
Dim strWordsA() As String = TextBox1.Text.Split(",")
Dim strWordsB() As String = TextBox2.Text.Split(",")
Dim str As String = TextBox1.Text
Dim arr As String() = TextBox1.Text.Split(","c)
For i As Integer = 0 To TextBox1.Text.Split(",").Length - 1
Dim index As Integer = str.IndexOf(strWordsA(i))
TextBox1.Text = str.Insert(index + 2, "," & strWordsB(i))
str = TextBox1.Text
Next
so if we have Textbox1.Text = 1,2,3,4,5,6,7,8,9 and Textbox2.Text = a,b,c,f,d,b,i,h, and so on... I need to display this in a 3rd textbox
Textbox3.Text = 1,a,2,b,3,c,4,f and so on
so do I combine these 2 strings?
the first element in the index displays it incorrectly, otherwise it seems to work ok.
Try this:
Private Function MergeStrings(s1 As String, s2 As String) As String
Dim strWordsA() As String = s1.Split(","c)
Dim strWordsB() As String = s2.Split(","c)
Dim i As Integer = 0
Dim OutputString As String = String.Empty
While i < strWordsA.Length OrElse i < strWordsB.Length
If i < strWordsA.Length Then OutputString &= "," & strWordsA(i)
If i < strWordsB.Length Then OutputString &= "," & strWordsB(i)
i += 1
End While
If Not OutputString = String.Empty Then Return OutputString.Substring(1)
Return OutputString
End Function
Usage:
Dim s As String = MergeStrings("1,2,3,4,5,6,7,8,9", "a,b,c,f,d,b,i,h")
You will need to add your own validation to allow for trailing commas or no commas etc but it should work with different length input strings
EDIT: amended as per Mary's comment

Split multi line in VB

I have a problem in split multi line in that it only splits the first line. I want to split all the lines.
Dim a As String
Dim b As String
Dim split = TextBox1.Text.Split(":")
If (split.Count = 2) Then
a = split(0).ToString
b = split(1).ToString
End If
TextBox2.Text = a
TextBox3.Text = b
You have to iterate all the lines in the textbox
For Each Ln As String In TextBox1.Lines
If Not String.IsNullOrEmpty(Ln) Then
Dim Lines() As String = Ln.Split(":"c)
If Lines.Length = 2 Then
TextBox2.Text &= Lines(0) & Environment.NewLine
TextBox3.Text &= Lines(1) & Environment.NewLine
End If
End If
Next
Edit- Updated to include condition checking to prevent index exceptions.
Edi2- It should be mentioned that drawing your strings into these textbox controls can take some time, it's not my place to judge your requirement, but you could optimize the routine by using collection based objects or stringbuilder.
IE:
Dim StrBldrA As New Text.StringBuilder
Dim StrBldrb As New Text.StringBuilder
For Each Ln As String In TextBox1.Lines
If Not String.IsNullOrEmpty(Ln) Then
Dim Lines() As String = Ln.Split(":"c)
If Lines.Length = 2 Then
StrBldrA.Append(Lines(0) & Environment.NewLine)
StrBldrb.Append(Lines(1) & Environment.NewLine)
End If
End If
Next
TextBox2.Text = StrBldrA.ToString
TextBox3.Text = StrBldrb.ToString

find the first number in the last line of text file and put ino integer variable visual basic

I am making a program that creates a text file that formats like so:
[1, * John, Doe, Family, 0002354561]
[2, * Jason, Doe, Obstetric, 0002358411]
[3, * Mikael, Doe, Pediatric, 0002352361]
[4, * Jamiel, Doe, Orthopedic, 0002354547]
What I need is a way to read the file and get the first number of the last line. and then put that into an integer.
I know i can read the first line like this:
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader(("..\..\..\Patients.txt"))
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)
But how do i read the last line and get the second char into an integer variable?
Here is an example of how you could accomplish the task, this puts all the numbers into an arraylist. Not that by converting it to an long(or integer) it will remove any leading zeros
Public Function GetDigits() As System.Collections.ArrayList
Dim numList As System.Collections.ArrayList
numList = New System.Collections.ArrayList()
Dim fileReader As System.IO.StreamReader
fileReader = My.Computer.FileSystem.OpenTextFileReader(("C:\Patients.txt"))
Dim stringReader As String
While Not fileReader.EndOfStream
stringReader = fileReader.ReadLine()
If Trim(stringReader) = "" Then Continue While
Dim strParts
strParts = Split(stringReader, Chr(44))
Try
numList.Add(Integer.Parse(Trim(Replace(strParts(UBound(strParts)), "]", ""))))
Catch
MsgBox("Could not parse string: " & stringReader)
End Try
End While
fileReader.Close()
GetDigits = numList
End Function
I'm using something like this
Dim lastLine As String = File.ReadLines(myFileName) _
.Where(Function(f As String) (Not String.IsNullOrEmpty(f))).Last.ToString
Dim startpos As Integer = lastLine.IndexOf(",", 3)
lastline = Substring(lastline, startpos).Trim
Dim firstchar as String = Substring(lastline,0,1)
Dim result As Integer
If Integer.TryParse(firstchar, result) Then Return result _
Else Return -1

Using Regex to match multiple html lines

Like the title says I have the html source that contains the code below. I am trying to grab the number 25 which can change so I think I would use the wildcard .* but I am not sure how to grab it because I the number is on its own line. I usually used one line of html then used the split method and split the double quotes then getting the value. I know how to do html webrequest and webresponse, so I have no problem getting the source. Any help with the regex expression would be appreciated.
<td class="number">25</td>
Edit: This is what I used to get the source.
Dim strURL As String = "mysite"
Dim strOutput As String = ""
Dim wrResponse As WebResponse
Dim wrRequest As WebRequest = HttpWebRequest.Create(strURL)
wrResponse = wrRequest.GetResponse()
Using sr As New StreamReader(wrResponse.GetResponseStream())
strOutput = sr.ReadToEnd()
'Close and clean up the StreamReader
sr.Close()
End Using
Instead of a Regex, it is possible to use a string search to pull out the text, e.g.:
Sub Main()
Dim html = "<td class=""number"">" + vbCrLf + "25" + vbCrLf + "</td>"
Dim startText = "<td class=""number"">"
Dim startIndex = html.IndexOf(startText) + startText.Length
Dim endText = "</td>"
Dim endIndex = html.IndexOf(endText, startIndex)
Dim text = html.Substring(startIndex, endIndex - startIndex)
text = text.Trim({CChar(vbLf), CChar(vbCr), " "c})
Console.WriteLine(text)
End Sub

Issue in splitting an array of strings

I'm using webrequests to retrieve data in a .txt file that's on my dropbox using this "format".
SomeStuff
AnotherStuff
StillAnother
And i'm using this code to retrieve each line and read it:
Private Sub DataCheck()
Dim datarequest As HttpWebRequest = CType(HttpWebRequest.Create("https://dl.dropboxusercontent.com.txt"), HttpWebRequest)
Dim dataresponse As HttpWebResponse = CType(datarequest.GetResponse(), HttpWebResponse)
Dim sr2 As System.IO.StreamReader = New System.IO.StreamReader(dataresponse.GetResponseStream())
Dim datastring() As String = sr2.ReadToEnd().Split(CChar(Environment.NewLine))
If datastring(datastring.Length - 1) <> String.Empty Then
For Each individualdata In datastring
MessageBox.Show(individualdata)
Console.WriteLine(individualdata)
Next
End If
End Sub
The problem is, the output is this:
It always adds a line break (equal to " " as i see as first character on each but the first line string) after the first line like:
http://img203.imageshack.us/img203/1296/gejb.png
Why this happens? I tried also replacing the Environment.Newline with nothing like this:
Dim newstring as String = individualdata.Replace(Environment.Newline, String.Empty)
But the result was the same... what's the problem here? I tried with multiple newline strings and consts like vbnewline, all had the same result, any ideas?
You are not splitting by NewLine since you are cutting off Environment.NewLine which is a string with CChar. You just have to use the overload of String.Split that takes a String() and a StringSplitOption:
So instead of
Dim text = sr2.ReadToEnd()
Dim datastring() As String = text.Split(CChar(Environment.NewLine))
this
Dim datastring() As String = text.Split({Environment.NewLine}, StringSplitOptions.None)
I suspect that your file contains a mix of NewLine+CarriageReturn (vbCrLf) and a simple NewLine (vbLf).
If this is the case then you could create an array of the possible separators
Dim seps(2) as Char
seps(0) = CChar(vbLf)
seps(1) = CChar(vbCr)
Dim datastring() As String = sr2.ReadToEnd().Split(seps, StringSplitOptions.RemoveEmptyEntries)
The StringSplitOptions.RemoveEmptyEntries is required because a vbCrLf creates an empty string between the two separators