469,267 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

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

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!
Nov 21 '05 #1
17 29609
"Carl Mercier" <no****@nospam.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
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
Nov 21 '05 #3
"Crouchie1998" <cr**********@spamcop.net> schrieb:
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
///

;-)

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
Herfried,

Mhm... I prefer a constant:

That is an answer *I* like *better*.

You know what I mean probably

:-)

Cor
Nov 21 '05 #5
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
Mhm... I prefer a constant:


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
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"

Herfried K. Wagner [MVP] wrote:
"Carl Mercier" <no****@nospam.com> schrieb:
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.

Nov 21 '05 #7
"Carl Mercier" <no****@nospam.com> schrieb:
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#.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
On 2005-04-23, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
"Carl Mercier" <no****@nospam.com> schrieb:
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.


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)?

Nov 21 '05 #9
"David" <df*****@woofix.local.dom> schrieb:
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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10
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/de...scapetopic.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

"Carl Mercier" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| 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!
Nov 21 '05 #11
Unscape works like a charm! EXACTLY what I was looking for.

Thank you -very- much for this!

Carl
Jay B. Harlow [MVP - Outlook] wrote:
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/de...scapetopic.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

"Carl Mercier" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| 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!

Nov 21 '05 #12
On 2005-04-23, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
"David" <df*****@woofix.local.dom> schrieb:
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.
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.


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.

Nov 21 '05 #13
On 2005-04-23, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
"Crouchie1998" <cr**********@spamcop.net> schrieb:
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


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?

Nov 21 '05 #14

Herfried
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:

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
Nov 21 '05 #15
"David" <df*****@woofix.local.dom> schrieb:
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


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #16
"David" <df*****@woofix.local.dom> schrieb:
> 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.

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.


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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #17
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")

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:us**************@tk2msftngp13.phx.gbl...
"David" <df*****@woofix.local.dom> schrieb:
>> 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.

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.


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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Frank Ratzlow | last post: by
3 posts views Thread by Rahul Agarwal | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.