473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 30618
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: User | last post by:
I am trying to use JDOM's SAXBuilder to parse an XML document that contains encoded latin-1 characters. After I parse the document, the special character Strings seem to be replaced with their...
8
by: Frank Ratzlow | last post by:
Hi folks, I have a list of entries where some entries contain the special character (quotes) within in the string. If I try to save this entry everything after the quotes is skipped. The reason...
4
by: Ewok | last post by:
let me just say. it's not by choice but im dealing with a .net web app (top down approach with VB and a MySQL database) sigh..... Anyhow, I've just about got all the kinks worked out but I am...
3
by: Rahul Agarwal | last post by:
Hi All We found that ASP.net is skipping the special characters passed in a URL ?context.Request.RawUrl RawUrl:...
11
by: ronrsr | last post by:
I have an MySQL database called zingers. The structure is: zid - integer, key, autoincrement keyword - varchar citation - text quotation - text the encoding and collation is utf-8
8
by: DierkErdmann | last post by:
Hi ! I know that this topic has been discussed in the past, but I could not find a working solution for my problem: sorting (lists of) strings containing special characters like "ä", "ü",......
1
AMT India
by: AMT India | last post by:
I am having a list of countries, among which some of them starts with German special characters ( like Umplot). I want to sort the list independent of this German characters. So that Umplot will come...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
5
by: Sobin Thomas | last post by:
Hi All, I want to pass a string that contains many special characters (: \ . _ etc) to another page in my website through query string. In my project I have a Gridview control ,in which there...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.