New line in VB.NET - 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

Related

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

find & vbCrLf & within a string - vb.net

In a string I have something like "First & vbCrLf & Name" - however, I want to take out the & vbCrLf & so it doesnt cause a line break.
I have done something like
If theString.Contains("& vbCrLf &") Then
' and replace, could do this above of course, but I just want it to go into the IF
End If
and
If theString.Contains("\n") Then
' and replace, could do this above of course, but I just want it to go into the IF
End If
and even "\r\n" but to no avail.
What am I missing?
If theString.Contains(vbCrLf) Then
'Do something
End If
Alternatively...
theString = theString.Replace(vbCrLf, "")
Try:
If theString.Contains(Environment.NewLine) Then
' Code goes here
End If
Remove the vbCrLf from the string literal in Contains.
testVal = testVal.Replace(vbCrLf, String.Empty).Replace("&", String.Empty)
Metacharacters not supported by VB.Net for Strings - can be used with RegEx and probably a few other .Net functions.
In your OP I think you intended:
If theString.Contains("& vbCrLf &") Then
to be
If theString.Contains(vbCrLf) Then
You can test for and replace in one command:
Dim s As String = vbCrLf
MsgBox(s.Length)
s = s.Replace(vbCrLf, "")
MsgBox(s.Length)

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.

Multilines In Richtextbox

Alright, I have a richtextbox that contains this.
line1
line2
As a test I used the code below to confirm that my program can read the lines but it doesn't.
If RichTextBox1.lines.Contains("Line1" & vbcrlf & "Line2") Then
MsgBox("hi")
End If
I've tried vbcrlf, environment.newline, char(32), vbcrlf & _.
thinking that either lines or contains is the problem.
RichTextBox.Lines returns an array with one element for each line of text. Contains("Line1" & VbCrLf & "Line2") will look for an element in the array that matches that string, but your array has one element with "line1" and a second element with "line2", not a single element with both. By the way, "Line1" will not match "line1", as there is a case difference between the two strings.
If you want to read the lines of the RichtTextBox, you can loop through it:
For Each line As String In RichTextBox1.Lines
' Do something here
Next
RichTextBox.Lines Property
have you try this
If RichTextBox1.lines.Contains("Line1" & vblf & "Line2") Then
MsgBox("hi")
End If

quotes in vb.net links

I want to make a link from what the user filepath that is given from an openfiledialog. But I can't get all the quotes in the right places
DOCTextBox.Text = "" & TitleTextBox.Text & ""
In order to put a " in a VB.Net string literal, you need to write "".
For example:
"<a href=""" & HttpUtility.HtmlAttributeEncode(OpenFileDialog1.FileName) _
& """target=_""blank"">" & HttpUtility.HtmlEncode(TitleTextBox.Text) & "</a>"
DOCTextBox.Text = "" & TitleTextBox.Text & ""
It sounds like you're trying to add quotes into the final string produced from the expression. In order to do that use a pair of double quotes "". For example
""
You can use triple quotes such as """