Minify HTML code into one line - vb.net

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

Related

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

VB.Net Writing to Txt File

I'm trying to write the content of my textbox to a txt file.
My code works fine but my error is, when I open txt file I see
writeline1writeline2writeline3
instead of
writeline1
writeline2
writeline3
my code;
result As List(Of String) = New List(Of String)
convertedText.Lines = result.ToArray()
My.Computer.FileSystem.WriteAllText(mypath & "\convertedcontent.txt", convertedText.Text, False)
Writing to .csv and many other file types work fine but I don't know how to break lines for text file.
Thanks in advance
I would use System.IO.File.WriteAllLines:
Dim path = System.IO.Path.Combine(mypath, "convertedcontent.txt")
System.IO.File.WriteAllLines(path, result)
Otherwise you need to append Environment.NewLine to each line, you can use String.Join:
System.IO.File.WriteAllText(path, String.Join(Environment.NewLine, result))
You need to add & vbCrLf to your strings (each line)
Not sure where you are getting your strings from.. but you will have to add the carrier return/Line Feed character to those strings, one at the end of every string.
Might just even loop through your array and add them there?
P.S. Some of the comments have quicker ways of getting there, but this is probably what happens behind the scenes...
for i = 0 to convertedText.Lines.count -1
convertedText.Lines(i) += vbCrLf
next

How can I remove the initial return when the multiple texts are added to the field?

I am asking the user to select a txt file from a specified folder on a server [This is in PowerPoint 2007], but I need to give them the option of selecting more than one, so I have a bit of conditional code to determine this.
One file selected uses this code:
oShape.TextFrame.TextRange.Text = Text
More than one file selected currently uses this:
oShape.TextFrame.TextRange.Text = oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf & Text
…but this results in an extra return space above it all in the field, which is a bit untidy.
Could anyone advise me on how I can modify this to only get the returns in between the two texts, but not at the beginning?
I am not entirely sure I got the problem, but I believe this will do:
oShape.TextFrame.TextRange.Text = iif(oShape.TextFrame.TextRange.Text <> "", oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf, "") & Text

How to copy mailItem.Body with line breaks to a website Textarea?

I'm using objItem.Body to fill in a website text area.
With HTML emails it adds spaces for line breaks or something, how do I fix that? Here is an example e-mail.
Using ie.document.getElementById("message").Value = objItem.Body you can see that two extra spaces/returns are added. It also adds HYPERLINK="mailto:xxxxx".
Converting to plain text with something like:
objitem.BodyFormat = olFormatPlain
objItem.Body = Replace(objItem.Body, " " & vbCrLf, vbCr)
retains the extra spacings and ruins the original email (I want to keep screenshots etc).
How can I get the mailItem.Body to transfer correctly?
The only thing left is removing Hyperlinks! IE HYPERLINg "mailto:email"email and HYPERLINK"website"website
This is how to get the objitem.body to do correct spacings on html emails..
ie.document.getElementById("message").Value = Replace(objItem.Body, vbCrLf & vbCrLf, vbCrLf)

New line in VB.NET

Why, when I do im my code:
"Land Location \\r\\n Roundoff (C)"
I see the \\r\\n and not a new line feeder at the output?
Any idea how to do that?
As I said I must have only one string there, without using a "&". Can I put that vbCrLf inside of my string somehow?
There is no \ escape codes in VB so you can't put a line break in a string literal. The only escape character in VB strings is the double quotation marks used to insert a quotation mark in a string.
You can use the VB constant for a Windows type line break:
"Land Location " & vbCrLf & " Roundoff (C)"
For the code to be platform independent, you should use the NewLine property instead:
"Land Location " & Environment.NewLine & " Roundoff (C)"
Whether you should use the platform independent code or not depends on the situation.
If you need it as a single string for some reason, you would have to use a marker for the line break, that you replace when you use the string:
Dim s As String = "Land Location \n Roundoff (C)"
s = Replace(s, "\n", Environment.NewLine)
May be "Land Location " & vbCR & vbLF .....
--
Edit: per #JeffSahol's comments, you can use string interpolation since VB 14, so it can be like
$"...{vbCrLf}..."
Instead of including the newline manually in the String use System.Environment.NewLine.
vbCrLf
vbCr
vbLf