VB.NET: Convert multiline string into single line - vb.net

I'm grabbing the HTML source code of a webpage and try to convert it to a single-line string but I can't.
This is my code:
Dim source As String = client.DownloadString("http://www.whocallsme.gr/el/master/lookup/19588")
source = source.Replace(vbCrLf, "")
I also tried using Environment.NewLine and vbNewLine instead of vbCrLf but the result remains the same.

Try this:
source = source.Replace(vbLf, "").Replace(vbCr, "")

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

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

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

Concatenating a complex string in VB. NET

I am using START-PROCESS to call MSTEST with multiple arguments that define the container and test settings, however I think it's choking on the way I'm concatenating this. Should I use some other method of constructing this string before putting it into START-PROCESS?
Dim rwSettings As String = "\\PerfvsCtlr2\LoadtestSettings\PerfVSCtlr2forRemote.testsettings"
Dim rwContainer As String = "\\PerfvsCtlr2\LoadTest\LoadTestDefs\Heifer_Interactive_Peak_Workload.loadtest"
Dim rwResults As String = Workload.txtRwResults.Text
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("VS110COMNTOOLS") & "..\Ide\MSTEST.EXE", "/Testsettings:""" & rwSettings & "" & " /Testcontainer:""" & rwContainer & "" & " /Resultsfile:""" & rwResults & "")
The problem is unknown currently, because process.start opens and closes the window far too quickly for me to catch any sort of error message. So my question is two-fold:
Does the above concatenation look correct? Is there a way I can get more information on either the final execution string Process.Start is putting together or the error message it's returning?
You can use Path.Combine to build paths and String.Format to build the arguments for Process.Start:
Dim rwSettings As String = "\\PerfvsCtlr2\LoadtestSettings\PerfVSCtlr2forRemote.testsettings"
Dim rwContainer As String = "\\PerfvsCtlr2\LoadTest\LoadTestDefs\Heifer_Interactive_Peak_Workload.loadtest"
Dim rwResults As String = "Workload.txtRwResults.Text"
Dim fileName = System.IO.Path.Combine(Environment.GetEnvironmentVariable("VS110COMNTOOLS"), "Ide\MSTEST.EXE")
Dim args = String.Format("/Testsettings:{0} /Testcontainer:{1} /Resultsfile:{2}", rwSettings, rwContainer, rwResults)
System.Diagnostics.Process.Start(fileName, args)
However, i must admit thar i'm not sure if this yields the desired result. It might give you an idea anyway.
I suspect that your problem is that you are not closing your quotation marks, for instance:
" /Testcontainer:""" & rwContainer & ""
Should be:
" /Testcontainer:""" & rwContainer & """"
Notice that the double-quotation mark at the end needs to be a quadruple quotation mark. Simply saying "" means an empty string.
Should you use something else? Probably. It would be more readable and efficient if you used StringBuilder or String.Format, but even so, you'll still have to fix the closing quotes issue.

Why Can't I do String.Replace() on a IO.File.ReadAllText() string?

I am using System.IO.FIle.ReadAllText() to get the contents of some template files that I created for email content. Then I want to do a Replace on certain tokens within the files so I can add dynamic content to the template.
Here is the code I have, it seems to me like it should work just fine...
Dim confirmUrl As String = Request.ApplicationPath & "?v=" & reg.AuthKey
Dim text As String = IO.File.ReadAllText( _
ConfigurationManager.AppSettings("sign_up_confirm_email_text").Replace("~", _
Request.PhysicalApplicationPath))
Dim html As String = IO.File.ReadAllText( _
ConfigurationManager.AppSettings("sign_up_confirm_email_html").Replace("~", _
Request.PhysicalApplicationPath))
text.Replace("%%LINK%%", confirmUrl)
text.Replace("%%NAME%%", person.fname)
html.Replace("%%LINK%%", confirmUrl)
html.Replace("%%NAME%%", person.fname)
For some reason I cannot get the %%LINK%% and %%NAME%% Replace() calls to work properly. I checked to see if it was encoding-related, so I made each file UTF-8. And also used the forced encoding overload of ReadAllText(String, Encoding) and still no dice. Any ideas?
The problem is that strings are immutable in .NET. So your Replace code should look like:
text = text.Replace("%%LINK%%", confirmUrl);
text = text.Replace("%%NAME%%", person.fname);
html = html.Replace("%%LINK%%", confirmUrl);
html = html.Replace("%%NAME%%", person.fname);