如何在C#中使用字符串中的特殊字符? [英] How to use special characters in strings like in C#?

查看:283
本文介绍了如何在C#中使用字符串中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否可以在VB.NET

字符串中使用特殊字符,如\ n或\t,就像在C#?我的猜测是否定的,但也许我有什么东西我不知道。


如果不可能,有人知道吗一个VB.NET函数(有人

必须已经编码)这将解释包含

这些特殊字符的字符串,并处理它们与在C#中处理它们一样? />

谢谢!

解决方案

" Carl Mercier" <无**** @ nospam.com> schrieb:

是否可以在VB.NET字符串中使用像\ n或\t这样的特殊字符,就像在C#中一样?我的猜测是否定的,但也许有一些我不知道的东西。


不,这是不受支持的。这是一个罕见的情况,你真的需要一个字符串文字中的\\\


。相反,使用''ControlChars.NewLine''将

包含特定于平台的新行字符串。 ''ControlChars''提供其他

字符,如''Cr'',''Lf''和''Tab''。


\\ \\\\

Dim s As String = _

" Hello" &安培; ControlChars.NewLine& _

" World"

///

如果不可能,有人知道VB.NET功能(有人
必须已经编码了这个,它会解释包含这些特殊字符的字符串,并在C#中处理它们吗?




支持C#字符串文字中的转义字符是一个编译器

功能,这意味着它只能在编译时工作。编译器

将用转义字符替换转义序列。 C#没有

提供了一种在运行时执行unescaping的方法。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://classicvb.org/petition/>


我倾向于做的是:


Dim nl As String = ControlChars.NewLine等。至少它会缩短

你的代码很多


Crouchie1998

BA(HONS)MCP MCSE

" Crouchie1998" < CR ********** @ spamcop.net> schrieb:

我倾向于说:

Dim nl As String = ControlChars.NewLine等。至少它会缩短你的代码很多




Mhm ...我更喜欢常数:


\\\

Const NL As String = ControlChars.NewLine

///


;-)


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL :http://classicvb.org/petition/>


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!

解决方案

"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/>


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


"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/>


这篇关于如何在C#中使用字符串中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆