Question

I'm trying to replace all the carriage return characters in a string obtained from a multi line text box in a Windows Form with the string ", <BR>" so that when I use the string in some HTML it displays correctly.

Function Blah(ByVal strInput As String) As String
  Dim rexCR As Object
  rexCR = CreateObject("VBScript.RegExp")
  rexCR.Pattern = "\r"
  rexCR.Global = True
  Blah = rexCR.Replace(strInput, ",<BR>")
End Function

Tried searching for any of the following characters and still no luck:
\r|\n|\r\c|\cM|\x0d

Problem seems to be the function/expression is not detecting any carriage returns in the text and I have no idea why? I know the function works as I can put a different expression in there as a test and it's OK

Any ideas?

Was it helpful?

Solution

Others have already provided good solutions to your problem. As a general remark, I would like to explain that VB.NET does not have string escape sequences (\n, \r, \t, ...) like C#. String literals in VB.NET are similar to verbatim string literals in C# -- the only character that can be escaped is the double-quote (by doubling it).

Instead, you have to use the VB constants (e.g. vbCrLf, vbTab) or .net constants (e.g. Environment.NewLine) and string concatenation ("Hello World" & vbCrLf instead of `"Hello World\n").

OTHER TIPS

How about normal strInput.Replace(vbCrLf,",<BR>") without regex?

How about:

Function Blah(ByVal strInput As String) As String
  return strInput.Replace(Environment.NewLine, "<br />")
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top