473,786 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
17 30673
"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.N ewLine' 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.Visu alBasic.Control Chars.NewLine'. The
current implementation will return a 'CRLF' sequence. However, it's
documented for 'System.Environ ment.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.N ewLine' too.

I am not sure if 'ControlChars.N ewLine' 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 #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 EscapeCharacter s(ByVal match As Match) As String
Select Case match.Value
Case "\b"
Return ControlChars.Ba ck
Case "\t"
Return ControlChars.Ta b
Case "\r"
Return ControlChars.Cr
Case "\v"
Return ControlChars.Ve rticalTab
Case "\f"
Return ControlChars.Fo rmFeed
Case "\n"
Return ControlChars.Lf
Case Else
Return match.Value.Sub string(1)
End Select
End Function

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

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

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******** ********@TK2MSF TNGP09.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.N ewLine' 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.Visu alBasic.Control Chars.NewLine'. The
current implementation will return a 'CRLF' sequence. However, it's
documented for 'System.Environ ment.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.N ewLine' too.


Yeah, Environment.New Line I knew about.
I am not sure if 'ControlChars.N ewLine' 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:
"Crouchie19 98" <cr**********@s pamcop.net> schrieb:
What I tend to do is say:

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


Mhm... I prefer a constant:

\\\
Const NL As String = ControlChars.Ne wLine


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.Ne wLine etc. At least then it will
shorten
your code quite a lot


Mhm... I prefer a constant:

\\\
Const NL As String = ControlChars.Ne wLine


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.N ewLine', 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.N ewLine' 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.Visu alBasic.Control Chars.NewLine'.
The
current implementation will return a 'CRLF' sequence. However, it's
documented for 'System.Environ ment.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.N ewLine' too.


Yeah, Environment.New Line I knew about.
I am not sure if 'ControlChars.N ewLine' 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.Environm ent,NewLine
Microsoft.Visua lBasic.ControlC hars.CrLf
Microsoft.Visua lBasic.ControlC hars.NewLine
Microsoft.Visua lBasic.Constant s.vbCrLf
Microsoft.Visua lBasic.Constant s.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******** ******@tk2msftn gp13.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.N ewLine' 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.Visu alBasic.Control Chars.NewLine'.
The
current implementation will return a 'CRLF' sequence. However, it's
documented for 'System.Environ ment.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.N ewLine' too.


Yeah, Environment.New Line I knew about.
I am not sure if 'ControlChars.N ewLine' 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
3163
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 unicode characters (e.g., the String "®" is replaced with a character that has a decimal value of 174); I was expecting that the SAXBuilder would preserve the String "®". Is it possible to instruct the SAX parser to preserve the special character...
8
22747
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 for this is quite obvious, since the resulting html tag looks like this: <option value="go "again" selected="selected">nochmal &quot;go</option>
4
5266
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 having trouble preserving data as it gets entered into the database. Primarily, quotes and special characters. Spcifically, I noticed it stripped out some double quotes and a "Registered" symbol &reg; (not the ascii but the actual character"
3
2110
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: "/iPower/GUI/DSHContactSearch.dset?@pvcClientName=&@pvcSurname=Røssland&@pvcFirstName=&@pvcErrMsg=" ?context.Request.Item("@pvcSurname") "Rssland"
11
10238
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
8853
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 "ä", "ü",... (german umlaute). Consider the following list: l = For sorting the letter "Ä" is supposed to be treated like "Ae", therefore sorting this list should yield
1
3057
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 after "t" in English alphabet. Thanx in advance,
3
10202
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. This is not a regular expression tutorial. Assumes you are already familiar with basic regular expression concepts and terminology. If not, you may want to read some regular expression tutorial. See the end of the article for links to online resources....
5
11167
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 is a hyperlink field.The Gridview 's datasource is set as a database table at runtime.The hyperlink filed's DataNavigateUrlFields is set to "RecordID" ,which is a field in my database table.The RecordID field contains many special characters.I...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9960
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.