473,770 Members | 2,137 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 30672
"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
3162
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
22746
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
10236
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
10201
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
11165
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
9619
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
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
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...
1
10038
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
9910
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
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.