如何忽略@转义字符(C#) [英] How to ignore @ escape character (C#)

查看:480
本文介绍了如何忽略@转义字符(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我为winforms控件添加了一个字符串属性。

这个字符串属性可以通过使用\\\\ n来获取多行文本;。

例如,Hello \\\\ nWorld

应该打印:

你好

世界


问题是c#_always_将我的字符串视为_path_(使用@ -quoted)。

打印:

你好\\\\ n世界


在这种情况下,我想摆脱这个愚蠢的''@'。

我如何纠正这个?

谢谢。

Minh

Hi,
I added a string property for my winforms control.
This string property can take multiple lines of text by using "\r\n".
For example, "Hello\r\nWorld"
SHOULD prints:
Hello
World

The problem is that c# _always_ treats my string as _path_ (using @-quoted).
It prints:
Hello\r\nWorld

I would like to get rid of this silly ''@'' in this case.
How do I correct this?
Thanks.
Minh

推荐答案

C#不会将字符串视为文字"除非它们是常量,前缀为'''''字符。


如果你的属性是String类型,那么你的代码必须假定传入set访问器的值是已经格式化

正确。换句话说,您的代码无法确定调用者是否使用了''@''符号。


如果要保留,请不要使用''''字符设置属性\\\\ n转义字符。


-

Dave Sexton
dave @ www..jwaonline..com

------------------------------ -----------------------------------------

" ;新人" <是ne ******** @ yahoo.com>在留言新闻中写道:21 ************************** @ posting.google.c om ...
C# does not treat strings as "literals" unless they are constants, prefixed with the ''@'' character.

If your property is of type String, then your code must assume that the value passed in to the set accessor is already formatted
correctly. In other words, your code cannot determine whether the caller used the ''@'' symbol.

Do not set your property using the ''@'' character if you want to preserve the "\r\n" escape characters.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Newcomer" <ne********@yahoo.com> wrote in message news:21**************************@posting.google.c om...

我为winforms控件添加了一个字符串属性。
这个字符串属性可以通过使用\\\\ n来获取多行文本。
例如,你好'\\\\
nWorld"
应该打印:
你好
世界

问题是c#_always_将我的字符串视为_path_(使用@ -quoted) 。
它打印:
你好我的世界

我想在这种情况下摆脱这种愚蠢的''@'。
如何我更正了吗?
谢谢。
Minh
Hi,
I added a string property for my winforms control.
This string property can take multiple lines of text by using "\r\n".
For example, "Hello\r\nWorld"
SHOULD prints:
Hello
World

The problem is that c# _always_ treats my string as _path_ (using @-quoted).
It prints:
Hello\r\nWorld

I would like to get rid of this silly ''@'' in this case.
How do I correct this?
Thanks.
Minh



问题是c#_always_ treats我的字符串为_path_(使用@ -quoted)。
打印:
Hello\\\\
World
The problem is that c# _always_ treats my string as _path_ (using @-quoted).
It prints:
Hello\r\nWorld




在哪里打印?如果您在调试器中查看字符串,它可能会以这样的方式显示



Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



Where do you print it? If you view the string in the debugger it may
appear that way.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


戴夫,谢谢你的回复。


我不会使用''@''字符串属性。


我想,这个问题只发生在我试图在_design_time_设置字符串值

时。 C#将检测\\\\ n并将其视为路径。


您可以通过使用带有文本编辑器的简单表单(textBox1)来测试它。

在设计时,设置文本value(textBox1)toHello \\\\ nWorld

(忽略引号)。然后在你的测试代码中,尝试

Console.WriteLine(textBox1.Text)。

你_will_只需打印一行。

这是我的问题。

再次感谢。

Ming


***通过Developersdex发送 http://www.developersdex.com ***
Dave, thanks for your reply.

I DON''T use ''@'' in the string property.

I think, this problem only happends when I tried to set the string value
at _design_time_. C# will detects "\r\n" and treats that as path.

You can test this by having a simple form with a text editor (textBox1).
At design time, set the text value (textBox1) to "Hello\r\nWorld"
(ignore the quotes). Then in your test code, try
Console.WriteLine(textBox1.Text).
You _will_ get just a single line of print out.

That is my problem.
Thanks again.
Ming

*** Sent via Developersdex http://www.developersdex.com ***


这篇关于如何忽略@转义字符(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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