find & vbCrLf & within a string - vb.net - 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)

Related

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.

AppendText won't append to the next line in a text file / vb

I'm making a program that lets you add student info to an existing CSV text file but whenever I use append text it adds the new info to part of the last line and then a new line.
I want it to do this:
John Doe,29,Male
John Doe,29,Male
It does this instead:
John Doe,29,MaleJo
hn Doe,29,Male
Note: there isn't actually an empty line between each set of info, I just wanted it to be easy to read when I posted it here.
Here is the code for that section:
Dim swVar As IO.StreamWriter = IO.File.AppendText(frmMain.fileName)
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()
Any help would be appreciated, thanks!
A handy tool in VS2010 (and others) is snippets - right click Insert Snippets... - lots of code patterns for typical tasks. In your case here is a modified snippet:
Sub AddToFile(textToAdd As String, filePath As String)
My.Computer.FileSystem.WriteAllText(filePath, textToAdd, True)
End Sub
You may want to check/add a vbNewLine to the text being added since one is not automatically added as with WriteLine.
Not a direct reply to your stated problem, but an alternate method.
See if something like this works better:
IO.File.AppendText(frmMain.fileName, vbNewLine & txtName.Text & "," & txtAge.Text & "," & gender & vbNewLine)
AppendText will open write and close all in one operation so there's no need for a separate streamwriter. It also doesn't add newlines so those must be added separately
Unless of course you are doing a series of writes to the same file then something like this would probably be more appropriate:
Dim swVar As New IO.StreamWriter(frmMain.fileName, True)
'write your lines here
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()

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

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