Creating a loop in StringBuilder to alter a text file - vb.net

I would be grateful with some help with reading a text file into a Richtext box. The code I have at present appends the first line of text as I want it but the rest of the lines of text do not alter. I need a loop to read to the end of file and display in Richtext box. the code i have at present is this:-
Dim FILE_NAME As String = "C:\Test.txt"
Dim sr As New System.IO.StreamReader(FILE_NAME)
RichTextBox1.Text = sr.ReadToEnd
Dim sb As New System.Text.StringBuilder(RichTextBox1.Text)
sb.Insert(5, " ")
sb.Insert(12, " ")
sb.Insert(18, " ")
sb.Insert(25, " ")
sb.Insert(29, " ")
sb.Insert(32, " ")
sb.Insert(37, " ")
sb.Insert(44, " ")
sb.Insert(45, " ")
RichTextBox2.Text = sb.ToString
sr.Close()

I think you just want RichTextBox1.LoadFile "C:/test.txt"
that should be a backslash in the file name but my keyboard doesn't have one on this pc

The reason for the spaces is because each line of text that have the same length characters with spaces needs to be seperated to make it more readable.The original text looks like this:-
17915WHITE BLUE 001.900116A T123456111
72451BLACK ORANGE000.500208 B A123456123 'worst case
72455BLACK WHITE 002.703501 C123456124
Needs to look like below.
17915:WHITE BLUE :001.9:001:16:A :T:123456:111
72451:BLACK ORANGE:000.5:002:08: B :A:123456:123
72455:BLACK WHITE :002.7:035:01: :C:123456:124
I can produce the first line to a text file but i cannot reproduce the rest of the lines of text i think i need a loop to keep reading over the text file until the file is read.

Related

Read a PDF Line by Line - iTextSharp

I'm not sure what is wrong with my code. It reads the PDF file, and grabs all the text, but every item is combined together into one string with no separator of any kind.
Sample:
"Houses: 2
Bedrooms: 3
Bathsroom 4"
will get read as "Houses: 2Bedrooms: 3Bathsroom 4"
I've searched through all of the examples to no avail. I've also tried LocationTextExtractionStrategy to no avail. I've tried using the .split method and no help.
Public Shared Function ParseAllPdfText(ByVal filepath As String)
Dim sbtxt, currenttext As String
sbtxt = ""
Try
Using reader As New PdfReader(filepath)
For intPages As Integer = 1 To reader.NumberOfPages
currenttext = PdfTextExtractor.GetTextFromPage(reader, intPages, New LocationTextExtractionStrategy())
currenttext = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.[Default], Encoding.UTF8, Encoding.[Default].GetBytes(currenttext)))
sbtxt = sbtxt & currenttext & vbcrlf
Next
End Using
Catch ex As Exception
MsgBox(" There was an error extracting text from the file: " & ex.Message, vbInformation, "Error Extracting Text")
End Try
Return sbtxt
Nevermind, this was an oversight on my part. I realized the lines are separated by Chr(10). Chr(10) does not create a new line in textboxes, which is where I was outputting my string. It DOES however create a new line in MsgBox. So if anyone else runs into this problem, chr(10) is the separator. :-)

How do you output commas from a csv file

I have created a program in visual studio 2010 which a user can input a sentence and then it will split the positions of the words and where they occur and the words. It then outputs them to two separate csv files.
If the user presses another button, it will regenerate the original inputted sentence.
Its seems to work with all types of punctuation apart from commas as they are the separator for csv files.
Is there a way of making the program output commas as well as other types of punctuation?
How i output to a CSV file:
Dim mydocpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Using WordStore As System.IO.TextWriter = System.IO.File.CreateText(mydocpath + "\Output\Words.csv")
Using IndexStore As System.IO.TextWriter = System.IO.File.CreateText(mydocpath + "\Output\Outputs.csv")
How I read from a CSV file:
Using CSVFileToRead As New FileIO.TextFieldParser(mydocpath + "\Output\Outputs.csv")
The current problem involving commas:
Dim output As String = Nothing
For Each Word As String In UserInput.Split(CChar(" "))
output &= (words.IndexOf(Word) + 1).ToString & ", "
Next
Label1.Text = output.Substring(0, output.Length - 2)

Minify HTML code into one line

My program is generating HTML code, which is placed afterwards into some string variable. This HTML code is ready to be placed in CSV file, so the entire code is surrounded by quotes, as well as all inner double quotes have additional quotes for escape. The result is the user can see it nicely formatted in output.
However, I have to convert this code to one line as assuming that my excel having trouble with this 'well formatted' HTML code as there are line-breaks. Therefore, I want before I place into CSV to make this HTML code into one line. Can you tell me how to achieve that?
You can replace line breaks by nothing to get a single lined output:
Dim TestString As String
TestString = " <body>" & vbCrLf & " some html" & vbCrLf & " </body>"
Dim SingleLined As String
SingleLined = Replace(TestString, vbCrLf, "")

Reading text file and skipping blank lines in vb.net

I have the below code which appends the content of a text file to my RichTextBox1.
Dim FileName = New DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CLIENT HISTORY\" & TextBox1.Text))
For Each ClientDetailsCHT As FileInfo In FileName.GetFiles("*.CHT", SearchOption.TopDirectoryOnly)
RichTextBox1.AppendText(File.ReadAllText(ClientDetailsCHT.FullName))
Next
' Send to printing sub
This works fine.
The problem I am having is that this text file sometimes contains blank lines and I would like to skip those blank lines so that the code only appends text to the RichTextBox.
How can I re-write my code to achieve this? I am using Visual Basic 2010.
You can loop through the lines and skip the ones that are blank. The following code skips lines that are empty or only contain white space. If you only want to skip empty lines, change IsNullOrWhiteSpace to IsNullOrEmpty.
Dim FileName = New DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CLIENT HISTORY\" & TextBox1.Text))
For Each ClientDetailsCHT As FileInfo In FileName.GetFiles("*.CHT", SearchOption.TopDirectoryOnly)
For Each line As String In File.ReadAllLines(ClientDetailsCHT.FullName)
If Not String.IsNullOrWhiteSpace(line) Then RichTextBox1.AppendText(line & vbCrLf)
Next
Next
' Send to printing sub

VB.NET - Split text doc using blank lines

I have a text doc with multiple lines but each "subject" is separated by a blank line.. like..
Block 1
Line 1
Line 2
Block 2
Line 1
Line 2
And so on. I have tried lots of variants using vbcr and the like.. but can't get each block to be separated by the "blank lines". The goal is to use each block's data individually.
Any help or direction would be greatly appreciated. Thanks in advance.
I'm a little unclear on what you're trying to do: are you trying to parse each "block" of data separately, and need to recognize a blank line as a blank line?
If that is the case, you could read each line as follows:
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim tempString As String = ""
Do While objReader.Peek() <> -1
tempString = objReader.ReadLine().Trim()
if tempString.equals("") Then ' we have a blank line ...
Else
' Do something else with the tempstring line
End If
Loop
There may be more sophisticated ways to do this, but this is what I'd do.
Try regular Expressions.
Import System.Text.RegularExpressions 'may be needed in your file to use below code...
Dim blocks() As String = Regex.Split(myData, "\n[ \t]*\n")
' regex looks for the occurrence of an enter char "\n" following by an optional amount of whitespace "[ \t]*" following by another enter character
You may also need to intermix some "\r" in that regex as a "carriage return \r" and a "new line \n" are sometimes mixed in different ways in data.
"\r\n" = vbCrLf
"\r" = vbCr
"\n" = vbLf
Dim subjectsWithLines() as string=split(stringThatYouReadFromFile,chr(10))
Now there are different kinds of BLANK lines, if chr(10) doesn't work then try using chr(13) or Environment.newline
ChicagoMike's answer works too, but due different kind of "BLANK LINES", use
tempString.Count<1 instead equals