473,686 Members | 2,271 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 #1
17 30660
"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.N ewLine' 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.Ne wLine & _
"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.Ne wLine etc. At least then it will shorten
your code quite a lot

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #3
"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
///

;-)

--
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\nWor ld!" 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.N ewLine' 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.Ne wLine & _
"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.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)?

Nov 21 '05 #9
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 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3159
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
22739
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
5260
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
2107
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
10216
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
8845
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
3051
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
10192
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
11156
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
8936
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8772
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
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...
1
6442
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
4312
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
4536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1941
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.