为什么人们在字符串前使用@符号? [英] Why do people use an @ sign before a string?
问题描述
您好,
我在一些代码中注意到人们有时会做一些事情
喜欢...
>
SomeFunction(@一个文本字符串);
@符号的目的是什么?我在传递
连接字符串时看到的最多(不可否认,在示例代码中,连接字符串
会更好地存储在真实应用中的其他位置),但我不知道我认为这是与此相关的
。
有人想开导我吗? TIA
-
Alan Silver
(此行下方添加的任何内容都与我无关)
@可以缩短输入,因为它声明此字符串是文字。
两个等效字符串的示例:
string URL = @" http://msnd.microsoft.com/newsgroups" ;;
string URL =" http:////msdn.microsoft.com//newsgroups" ;;
在第二个字符串中,每个whack都必须被转义。 />
---
Gregory A. Beamer
MVP; MCP:+ I,SE,SD,DBA
***************************
在盒子外面思考!
*************************** >
" Alan Silver"写道:
你好,
我注意到在一些代码中人们有时会做的事情像
一样......
SomeFunction(@一个文本字符串);
@符号的目的是什么?我在传递
连接字符串时看到的最多(在示例代码中,因为连接字符串
会更好地存储在真实应用程序的其他位置),但我不认为它是
任何人都在乎开导我? TIA
-
Alan Silver
(此行下面添加的任何内容都与我无关)
> @可以缩短输入,因为它声明此字符串是文字。两个等效字符串的示例:
string URL = @" http://msnd.microsoft.com/newsgroups" ;;
string URL =" http:////msdn.microsoft.com//newsgroups" ;;
在第二个字符串中,每个whack都必须被转义。
哦,不是那么聪明!我不知道为什么你需要加倍/ / $
第二个例子,它是否用于特殊的东西?我认为
是反斜杠,比如嵌入一个换行符。
嵌入引号怎么样,这会有帮助吗?
感谢您的回复。
***************************
在外面思考盒子!
当我长到足以看到顶部时,我会尝试思考!!
*********** ****************
Alan Silver写道:
你好,
我注意到在一些代码中人们有时会做的事情像
一样......
SomeFunction(@一个文本字符串);
@符号的目的是什么?我在传递
连接字符串时看到的最多(在示例代码中,因为连接字符串
会更好地存储在真实应用程序的其他位置),但我不认为它是
任何人都在乎开导我? TIA
-
Alan Silver
(此行下面添加的任何内容都与我无关)
-
Alan Silver
(此行以下添加的任何内容都与我无关)
< blockquote>是的,你是对的。我相信格雷戈里的意思。这些应该是
退格,比如一条路径。
你问的是逃避字符串。
string s ="他说,\" No!\"" ;;
string s = @"他说," No!""" ;
注意双倍双引号。
bill
" Alan Silver" <人********* @ nospam.thanx>在消息中写道
news:uK ************** @ nospamthankyou.spam ...
@可以缩短输入,因为它声明此字符串是文字。
两个等效字符串的示例:
字符串URL = @" http://msnd.microsoft.com/newsgroups" ;;
字符串URL =" http:/// /msdn.microsoft.com//newsgroups" ;;
在第二个字符串中,每个whack都必须被转义。
哦,不,不聪明!!我不确定为什么你需要加倍/在第二个例子中,它是否用于特殊的东西?我认为反斜杠是,比如嵌入一个换行符。
如何嵌入引号,这会有帮助吗?
感谢您的回复。
***************************
在盒子外面思考!
当我长到足以看到顶部时,我会尝试思考!!
************ ***************
Alan Silver写道:
你好,
我注意到在一些代码中人们有时会做的事情像
一样......
SomeFunction(@一个文本字符串);
@符号的目的是什么?我在传递
连接字符串时看到的最多(在示例代码中,因为连接字符串
会更好地存储在真实应用程序的其他位置),但我不认为它是
任何人都在乎开导我? TIA
-
Alan Silver
(此行下面添加的任何内容都与我无关)
-
Alan Silver
(此行下面添加的任何内容都与我无关)
Hello,
I have noticed in a few bits of code that people sometimes do things
like...
SomeFunction(@"a text string");
What is the purpose of the @ sign? I have seen it most when passing
connection strings (admittedly in example code, as connection strings
would be better stored elsewhere in a real app), but I don''t think it''s
related to that.
Anyone care to enlighten me? TIA
--
Alan Silver
(anything added below this line is nothing to do with me)
The @ can shorten typing, as it states "This string is a literal". Example of
two equivalent strings:
string URL = @"http://msnd.microsoft.com/newsgroups";
string URL = "http:////msdn.microsoft.com//newsgroups";
In the second string, every whack has to be escaped.
---
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"Alan Silver" wrote:
Hello,
I have noticed in a few bits of code that people sometimes do things
like...
SomeFunction(@"a text string");
What is the purpose of the @ sign? I have seen it most when passing
connection strings (admittedly in example code, as connection strings
would be better stored elsewhere in a real app), but I don''t think it''s
related to that.
Anyone care to enlighten me? TIA
--
Alan Silver
(anything added below this line is nothing to do with me)
>The @ can shorten typing, as it states "This string is a literal". Example oftwo equivalent strings:
string URL = @"http://msnd.microsoft.com/newsgroups";
string URL = "http:////msdn.microsoft.com//newsgroups";
In the second string, every whack has to be escaped.
Ooh, ain''t that clever!! I''m not sure why you need to double the / in
the second example though, is it used for something special? I thought
the backslash was, say for embedding a newline character.
How about embedding quotes, would this help?
Thanks for the reply.
***************************
Think Outside the Box!
When I grow big enough to see over the top, I''ll try thinking!!
***************************
"Alan Silver" wrote:Hello,
I have noticed in a few bits of code that people sometimes do things
like...
SomeFunction(@"a text string");
What is the purpose of the @ sign? I have seen it most when passing
connection strings (admittedly in example code, as connection strings
would be better stored elsewhere in a real app), but I don''t think it''s
related to that.
Anyone care to enlighten me? TIA
--
Alan Silver
(anything added below this line is nothing to do with me)
--
Alan Silver
(anything added below this line is nothing to do with me)
Yes, you are correct. I am sure Gregory meant that. Those should have been
backspaces such as a unc path.
You were asking about escaping string.
string s = "He said, \"No!\"";
string s = @"He said, ""No!""";
Notice the double double quotes.
bill
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:uK**************@nospamthankyou.spam...The @ can shorten typing, as it states "This string is a literal". Example oftwo equivalent strings:
string URL = @"http://msnd.microsoft.com/newsgroups";
string URL = "http:////msdn.microsoft.com//newsgroups";
In the second string, every whack has to be escaped.
Ooh, ain''t that clever!! I''m not sure why you need to double the / in
the second example though, is it used for something special? I thought
the backslash was, say for embedding a newline character.
How about embedding quotes, would this help?
Thanks for the reply.***************************
Think Outside the Box!
When I grow big enough to see over the top, I''ll try thinking!!***************************
"Alan Silver" wrote:Hello,
I have noticed in a few bits of code that people sometimes do things
like...
SomeFunction(@"a text string");
What is the purpose of the @ sign? I have seen it most when passing
connection strings (admittedly in example code, as connection strings
would be better stored elsewhere in a real app), but I don''t think it''s
related to that.
Anyone care to enlighten me? TIA
--
Alan Silver
(anything added below this line is nothing to do with me)
--
Alan Silver
(anything added below this line is nothing to do with me)
这篇关于为什么人们在字符串前使用@符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!