472,348 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 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 30442
"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...
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...
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...
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...
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...
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...
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...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.