Display “&” from text file - vb.net

I am trying to display some text from a .txt file in a label, but the text has an ampersand in it which will not display. I am getting the text by reading singles lines using a System.IO.StreamReader. I tried using a backslash as an escape character, but all that did was display a backslash in the label.

Use another & as an escape character.
&&

Dim sr As New StreamReader("C:\yada.txt")
Dim line As String = sr.ReadLine
Label1.Text = line.Replace("&", "&&")

Related

how to save text from richtext box to text document in vb.net

I'm taking value from richtextbox and i want to save it in new text document
I Want to save text of rich text box in text document as i write in rich text box but it stores like shown in image
My Code
Using Sw As StreamWriter = File.CreateText(path)
Dim number As String = RichTextBox1.Text
Sw.WriteLine(number)
End Using
Please help
The problem here is that both the TextBox and the RichTextBox control uses LF (Line Feed) as their newline format, but Windows's newline format is actually CR + LF (Carriage Return + Line Feed), which is what Notepad expects it to be.
The line breaks are there, Notepad just doesn't render them.
To fix this you can either replace all LFs with Environment.NewLine (which adapts to the current system) before saving:
Dim number As String = RichTextBox1.Text.Replace(vbLf, Environment.NewLine)
Sw.Write(Number)
...or you can save it line-by-line instead using the StreamWriter.WriteLine() method (which uses CR + LF for line breaks):
Using Sw As StreamWriter = File.CreateText(path)
For Each Line As String In RichTextBox1.Lines
Sw.WriteLine(Line)
Next
End Using
Read more:
Newline - Wikipedia (Line Feeds)
Carriage return - Wikipedia
After each Write, perform .Write(Environment.NewLine)

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

Read and Write to specific line in textfile with VB.Net

So here is my problem:
1st I need to read a text file and get the line number 5
System.IO.File.ReadAllText("E:\myFile.txt")
My text file its something like this:
ABCDE "2015"
GDFTHRE "0.25 0.25"
TRYIP "192.168.1.6"
WIDTH "69222"
ORIGIN "200"
So, what i need is to replace the value 200, lets say 250 and keep the line as this: ORIGIN "250"
I have tryed with the Replace but i can't get it.
If your text file is divided into lines and you only want to look at the 5th line, you can use ReadAllLines to read the lines into an array of String. Process line 4 (the 5th line) and use WriteAllLines to re-write the file. The following example checks that the file contains at least 5 lines and that the 5th line begins with "ORIGIN "; if so it replaces the line with ORIGIN "250" and re-writes the file.
Dim filePath As String = "E:\myFile.txt"
Dim lines() As String = System.IO.File.ReadAllLines(filePath)
If lines.Length > 4 AndAlso lines(4).StartsWith("ORIGIN ") Then
lines(4) = "ORIGIN ""250"""
System.IO.File.WriteAllLines(filePath, lines)
End If
You can simply replace the text and then write everything back to the file again:
Dim content As String
' read all text from the file to the content variable
content = System.IO.File.ReadAllText("E:\myFile.txt")
' replace number, text, etc. in code
content = content.Replace("<string to replace>","<replace with>")
' write new text back to the file (by completely overwriting the old content)
System.IO.File.WriteAllText("E:\myFile.txt",content)

rich textbox combines all lines into one when saved as txt file?

I have a rich textbox in vb.net, which contains several lines of text however when I try and save the text to a .txt file all the lines are combined into one???
How can I overcome this?
what i did try was:
Dim MYLINES As Object
For Each MYLINES In RichTextBox1.Text
objWriter.WriteLine(MYLINES.ToString & Environment.NewLine)
Next
objWriter.Close()
however this simply placed every single character on a difrent line...
Save it as a .RTF file (instead of .TXT), wich can process the line breaks directly. Thats an option.
Or you can wirte the lines individually:
Dim sw As New System.IO.StreamWriter(sFileName)
For Each sLine as String in TextBox1.Lines
sw.WriteLine(sLine)
Next
sw.Close()
The method WriteLine already adds a line break at the end. It more or less the same as:
.Write(sLine & Enviroment.NewLine())

Creating a loop in StringBuilder to alter a text file

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.