How to use special characters in strings like in C#?

  • Thread starter Thread starter Carl Mercier
  • Start date Start date
C

Carl Mercier

Hi,

Is it possible to use special characters like \n or \t in a VB.NET
string, just like in C#? My guess is NO, but maybe there's something I
don't know.

If it's not possible, does anybody know of a VB.NET function (somebody
must have coded this already) that will interpret strings containings
those special characters, and handle them the same as in C#?

Thanks!
 
Carl Mercier said:
Is it possible to use special characters like \n or \t in a VB.NET string,
just like in C#? My guess is NO, but maybe there's something I don't know.

No, that's not supported. It's a rare case that you really need a "\n"
inside a string literal. Instead, use 'ControlChars.NewLine' which will
contain a platform-specific new-line string. 'ControlChars' provides other
characters like 'Cr', 'Lf', and 'Tab' too.

\\\
Dim s As String = _
"Hello" & ControlChars.NewLine & _
"World"
///
If it's not possible, does anybody know of a VB.NET function (somebody
must have coded this already) that will interpret strings containings
those special characters, and handle them the same as in C#?

The support for escaped characters in C# string literals is a compiler
feature, which means that it will only work at compile time. The compiler
will replace the escape sequence with the escaped character. C# doesn't
provide a way to perform the unescaping at runtime.
 
What I tend to do is say:

Dim nl As String = ControlChars.NewLine etc. At least then it will shorten
your code quite a lot

Crouchie1998
BA (HONS) MCP MCSE
 
Crouchie1998 said:
What I tend to do is say:

Dim nl As String = ControlChars.NewLine etc. At least then it will shorten
your code quite a lot

Mhm... I prefer a constant:

\\\
Const NL As String = ControlChars.NewLine
///

;-)
 
Cor,

Cor Ligthert said:
That is an answer *I* like *better*.

You know what I mean probably

I have to correct my answer: Using a constant is the "best" solution in
this case:

\\\
Dim t1 As String = "j"
Const t2 As String = "j"
Dim x1 As String = "a" & t1
Dim x2 As String = "a" & t2
///

Corresponding IL code:

\\\
IL_0001: ldstr "j"
IL_0006: stloc.0
IL_0007: ldstr "a"
IL_000c: ldloc.0
IL_000d: call string [mscorlib]System.String::Concat(string,
string)
IL_0012: stloc.1
IL_0013: ldstr "aj"
IL_0018: stloc.2
///

When using a constant, the compiler will be able to perform the
concatenation at compile time, when using a string variable, the
concatenation is done at runtime.
 
Hi,

I know about .NewLine, but I need to store strings in a file (on a
single line), and I wanted to make use of special characters like in C#.
IMO, this difference with C# is pretty stupid. MS should allow us to
use it in VB as well.

If anyone knows of a VB function that will convert strings with special
characters like \n to a string with a .NewLine character in there, that
would be great.

EX:

Original: "Hello\nWorld!" gets converted to "Hello" + NewLine + "World"
 
Carl Mercier said:
I know about .NewLine, but I need to store strings in a file (on a single
line), and I wanted to make use of special characters like in C#. IMO,
this difference with C# is pretty stupid. MS should allow us to use it in
VB as well.

You need to separate between compiler behavior and reading data from a file.
C# doesn't have any advantages in the latter case, if the file contains the
string "\r\n", the sequence of characters "\", "r", "\", "n" will be
contained in the string, not a 'CRLF' sequence.
If anyone knows of a VB function that will convert strings with special
characters like \n to a string with a .NewLine character in there, that
would be great.

You will have to do the replacement yourself, the same way as you have to do
it in C#.
 
No, that's not supported. It's a rare case that you really need a "\n"
inside a string literal. Instead, use 'ControlChars.NewLine' which will
contain a platform-specific new-line string.

Is that actually documented someplace, that NewLine will return a
platform-specific string? Does anyone know if mono does this (I've
never used the vb stuff in mono)?
 
David said:
Is that actually documented someplace, that NewLine will return a
platform-specific string? Does anyone know if mono does this (I've
never used the vb stuff in mono)?

It's not documented for 'Microsoft.VisualBasic.ControlChars.NewLine'. The
current implementation will return a 'CRLF' sequence. However, it's
documented for 'System.Environment.NewLine' ("Gets the newline string
defined for this environment", "The property value is a constant customized
specifically for the current platform"), and I think that this would make
sense for 'ControlChars.NewLine' too.

I am not sure if 'ControlChars.NewLine' in mono will return an
environment-specific new-line string, but I would expect it to do so.
 
Carl,
| Is it possible to use special characters like \n or \t in a VB.NET
| string, just like in C#?
As the others have pointed out \n & \t are a compile time feature of C#, not
a runtime feature.


It sounds like you want a runtime feature that will convert the C# escape
sequences into valid chars. I don't know how complete it is, RegEx.Unescape
will unescape most if not all of the C# escape sequences.

http://msdn.microsoft.com/library/d...regularexpressionsregexclassunescapetopic.asp


Alternatively I would consider using a RegEx.Replace with a MatchEvaluator
function.

Something like:

Private Shared Function EscapeCharacters(ByVal match As Match) As String
Select Case match.Value
Case "\b"
Return ControlChars.Back
Case "\t"
Return ControlChars.Tab
Case "\r"
Return ControlChars.Cr
Case "\v"
Return ControlChars.VerticalTab
Case "\f"
Return ControlChars.FormFeed
Case "\n"
Return ControlChars.Lf
Case Else
Return match.Value.Substring(1)
End Select
End Function

Const pattern As String = "\\."
Static parser As New Regex(pattern, RegexOptions.Compiled)

Dim input As String
Dim output As String
input = parser.Replace(input, AddressOf EscapeCharacters)

The advantage of RegEx.Replace is you have control over how the escape
sequences are defined. For example you could use {Back}, {Tab}, {Cr}, ...
instead.

With effort it should be easy to add support for octal (\040), hex (\x20),
control (\cC), unicode (\u0020) sequences.

Hope this helps
Jay

| Hi,
|
| Is it possible to use special characters like \n or \t in a VB.NET
| string, just like in C#? My guess is NO, but maybe there's something I
| don't know.
|
| If it's not possible, does anybody know of a VB.NET function (somebody
| must have coded this already) that will interpret strings containings
| those special characters, and handle them the same as in C#?
|
| Thanks!
 
Unscape works like a charm! EXACTLY what I was looking for.

Thank you -very- much for this!

Carl
 
It's not documented for 'Microsoft.VisualBasic.ControlChars.NewLine'. The
current implementation will return a 'CRLF' sequence. However, it's
documented for 'System.Environment.NewLine' ("Gets the newline string
defined for this environment", "The property value is a constant customized
specifically for the current platform"), and I think that this would make
sense for 'ControlChars.NewLine' too.

Yeah, Environment.NewLine I knew about.
I am not sure if 'ControlChars.NewLine' in mono will return an
environment-specific new-line string, but I would expect it to do so.

I just checked the mono source to answer my own question, and it returns
CRLF for .NewLine. That makes sense, since doing otherwise would make
NewLine the only platform-independent member of ControlChars.
 
Mhm... I prefer a constant:

\\\
Const NL As String = ControlChars.NewLine

Isn't that kinda redundant, since NewLine is already a constant? And if
you want something shorter, why not good ol' vbCrLf, which would seem to
be much more recognizable than making up a new abbreviation?
 
Herfried
I have to correct my answer: Using a constant is the "best" solution in
this case:

When using a constant, the compiler will be able to perform the
concatenation at compile time, when using a string variable, the
concatenation is done at runtime.
And this one *I* like even *better*

Cor
 
David said:
Isn't that kinda redundant, since NewLine is already a constant? And if
you want something shorter, why not good ol' vbCrLf, which would seem to
be much more recognizable than making up a new abbreviation?

ACK. The "prefer" in my sentence was related to variable vs. constant only.
I always use 'ControlChars.NewLine', or I import 'ControlChars' and use the
unqualified 'NewLine'. Note that there is a 'vbNewLine' constant too.
 
David said:
Yeah, Environment.NewLine I knew about.


I just checked the mono source to answer my own question, and it returns
CRLF for .NewLine. That makes sense, since doing otherwise would make
NewLine the only platform-independent member of ControlChars.

I don't think that this is a valid reason for making 'NewLine' not platform
dependent.

I remember that once 'vbNewLine' returned different character sequences on
Windows and on the Macintosh ("Platform-specific newline character; whatever
is appropriate for the platform"). A similar statement can be found in the
VB6/VBA documentation. The VB.NET documentation IMO doesn't explicitly
state that, mainly because VB.NET is not standardized and Microsoft doesn't
provide an implementation for another platform than Windows.
 
For what it's worth:

System.Environment,NewLine
Microsoft.VisualBasic.ControlChars.CrLf
Microsoft.VisualBasic.ControlChars.NewLine
Microsoft.VisualBasic.Constants.vbCrLf
Microsoft.VisualBasic.Constants.Newline

all return ChrW(13) & ChrW(10)

The IL for them, respectively is:

.method public hidebysig specialname static string get_NewLine() cil
managed
{
// Code Size: 6 byte(s)
.maxstack 8
L_0000: ldstr "\r\n"
L_0005: ret
}

.field public static literal string CrLf = string("\r\n")

.field public static literal string NewLine = string("\r\n")

.field public static literal string vbCrLf = string("\r\n")

.field public static literal string vbNewLine = string("\r\n")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top