C#字符串 [英] C# strings
问题描述
我有几个关于C#字符串的问题。
1)有时我在别人的代码中看到一个字符串,如:
string foo = @" bar";
''''为你做什么?
2)是否有表演如果我开始将字符串连接在一起
与+运算符?在Java中,他们不鼓励这样的代码:
string command =" CMD" + var1 +" " + var2;
原因是因为在此期间创建了
的所有临时String对象。解决方案是使用如下的StringBuffer:
StringBuffer command = new StringBuffer(" CMD");
command.append(var1);
command.append('''');
command.append(var2);
System.out.println(command.toString ());
这比上面的代码便宜,因为String对象不是左边和后面创建的
对。 C#有同样的问题吗?我注意到了
C#有一个StringBuilder类,就像Java StringBuffer类一样。
我应该在上面需要的情况下使用StringBuffer
将多个字符串连接成一个字符串?
I have a couple of questions regarding C# strings.
1) Sometimes I see a string in someone else''s code like:
string foo = @"bar";
What does the ''@'' do for you?
2) Is there a performance loss if I start concatenating strings together
with the + operator ? In Java, they discouraged code like this:
string command = "CMD " + var1 + " " + var2;
The reason is because of all of the temporary String objects that get
created in the meantime. The solution is to use a StringBuffer like so:
StringBuffer command = new StringBuffer("CMD ");
command.append(var1);
command.append('' '');
command.append(var2);
System.out.println(command.toString());
This is less expensive than the above code because String objects are not
being created left & right. Does C# have the same problem? I noticed that
C# has a StringBuilder class which is just like the Java StringBuffer class.
Should I use StringBuffer in cases like the above where I need to
concatenate multiple strings to one string?
推荐答案
嗨Trevor,
请参阅回复在线。
Joe
-
http://www.csharp-station.com
" Trevor" < TR **** @ nospam.com>在消息中写道
新闻:uC ************** @ TK2MSFTNGP12.phx.gbl ...
Hi Trevor,
Please see response in-line.
Joe
--
http://www.csharp-station.com
"Trevor" <tr****@nospam.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
1)有时我看到别人的代码中的一个字符串,如:
string foo = @" bar";
''''为你做什么?
这是一个逐字字符串文字,它保留
双引号之间的字符。它的一个用途是当你想让文件路径更加可读时。例如,在普通字符串中,您将像这样转义路径
分隔符:
" c:\\MyDir \\ MySubDir \\MyFile.cs"
相反,你可以这样做:
@" c:\ MyDir \ MySubDir \ MyFile.cs"
此外,您有时可以这样做:
@"
这是我想要在
控制台上用换行显示的一些文字
"
而不是:
"这是我想要在带有换行符的\ n控制台上显示的一些文字
或其他不太可读的东西。
2)如果我开始使用+运算符将字符串串联起来,是否存在性能损失?在Java中,他们不鼓励这样的代码:
string command =" CMD" + var1 +" " + var2;
是的,这些连接存在性能损失,因为在每个连接上创建了新的
字符串对象。字符串类型是
不可变。
我应该在上面的情况下使用StringBuffer,我需要将多个字符串连接到一个字符串吗?
1) Sometimes I see a string in someone else''s code like:
string foo = @"bar";
What does the ''@'' do for you?
That''s a verbatim string literal, which preserves characters between the
double quotes. One of its uses is when you want to make file paths more
readable. For example, in a normal string, you would escape the path
separator like this:
"c:\\MyDir\\MySubDir\\MyFile.cs"
Instead, you can do this:
@"c:\MyDir\MySubDir\MyFile.cs"
Also, you can sometimes do something like this:
@"
This is some text that I
want to show on the
console with line feeds
"
Instead of:
"This is some text that I\n want to show on the\n console with line feeds"
or something else less readable.
2) Is there a performance loss if I start concatenating strings together
with the + operator ? In Java, they discouraged code like this:
string command = "CMD " + var1 + " " + var2;
Yes, there is a performance penalty with these concatenations because of new
string objects being created on each concatenation. The string type is
immutable.
Should I use StringBuffer in cases like the above where I need to
concatenate multiple strings to one string?
你应该使用StringBuilder。我使用经验法则,当我有4个或更多b $ b或更多串联时,我将使用StringBuilder。
You should use the StringBuilder. I use a rule-of-thumb that when I have 4
or more concatenations, I will use a StringBuilder instead.
" Trevor" < TR **** @ nospam.com>在消息中写道
新闻:uC ************** @ TK2MSFTNGP12.phx.gbl ...
"Trevor" <tr****@nospam.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
我有几个关于C#字符串的问题。
1)有时候我会在别人的代码中看到一个字符串,如:
string foo = @" bar" ;;
''''为你做什么?
2)如果用+运算符开始将字符串连接在一起,是否存在性能损失?在Java中,他们不鼓励这样的代码:
string command =" CMD" + var1 +" " + var2;
I have a couple of questions regarding C# strings.
1) Sometimes I see a string in someone else''s code like:
string foo = @"bar";
What does the ''@'' do for you?
2) Is there a performance loss if I start concatenating strings together
with the + operator ? In Java, they discouraged code like this:
string command = "CMD " + var1 + " " + var2;
Hello Trevor,
#2:C#非常相似,提供了字符串构建器类对于那个。
我认为,如果你只是连接几个变量,那么
的表现并不是那么难。它正在构建大字符串,其中字符串
builder非常方便。关于这个话题已经有很多讨论了,但是对于
我自己,我使用字符串构建器,如果我连接超过10个项目。
#1:@符号使编译器按原样处理字符串,而不是
查找转义字符和诸如此类的东西。我只知道
,如果我想对文件夹和文件名进行硬编码,或类似的东西,我必须使用@" [string]"或者它不能正常工作。当然别人
会给出一个更确定的答案。期待自己。
Eric
Hello Trevor,
#2: C# is much the same, and the string builder class is provided for that.
I think though that if you are just concatenating a couple of variables, the
performance hit isn''t so hard. It''s building large strings where string
builder is handy. There''s been lots of discussion on this topic, but for
myself, I use string builder if I''m concatenating more than say 10 items.
#1: The @ sign causes the compiler to treat the string as it is, and not
look for escape characters and whatnot, or something like that. I only know
that if I want to hardcode a folder and filename, or something like that, I
have to use the @"[string]" or it doesn''t work properly. Surely someone else
will give a more definitive answer. Looking forward to that myself.
Eric
Trevor,
见内联。
" Trevor" < TR **** @ nospam.com>在消息中写道
新闻:uC ************** @ TK2MSFTNGP12.phx.gbl ...
Trevor,
See inline.
"Trevor" <tr****@nospam.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
我有几个关于C#字符串的问题。
1)有时候我会在别人的代码中看到一个字符串,如:
string foo = @" bar" ;;
''''为你做什么?
amperstand意味着将字符串作为文字,而不是解释
转义码。例如,在使用路径时,您必须为每个路径分隔符使用\>
两次,因为\和需要转义,如下:
" c:\\\\ nmp"
但是,使用字符串文字,那里没有逃脱角色,所以你不需要担心
。您可以这样写:
@" c:\ temp"
2)如果我开始连接字符串,是否会导致性能下降与+运营商一起?在Java中,他们不鼓励这样的代码:
string command =" CMD" + var1 +" " + var2;
原因是因为在此期间创建了所有临时String对象。解决方案是使用如下的StringBuffer:
StringBuffer command = new StringBuffer(" CMD");
command.append(var1);
command.append( '''');
command.append(var2);
System.out.println(command.toString());
这比上面的代码便宜因为String对象不是左边和右边创建的。对。 C#有同样的问题吗?我注意到
,C#有一个StringBuilder类,就像Java StringBuffer
类一样。我应该在上面的情况下使用StringBuffer,我需要将多个字符串连接到一个字符串吗?
一般来说,如果你要做很多字符串操作,那么你应该使用StringBuilder实例来完成它。它只比连接字符串更快。
。应该注意以下几行:
string command =" CMD" + var1 +" " + var2;
实际上将被编译成:
string command = String.Concat(" CMD",var1, "",var2);
在封面下,Concat使用StringBuilder实例来完成
工作。此外,应该注意的是,编译器将两个字符串文字的串联处理为单个实例。换句话说:
" Hello" +在那里
汇编成:
Hello there
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com
I have a couple of questions regarding C# strings.
1) Sometimes I see a string in someone else''s code like:
string foo = @"bar";
What does the ''@'' do for you?
The amperstand means to take the string as a literal, and not interpret
escape codes. For example, when working with paths, you have to use "\"
twice for each path separator, because "\" needs to be escaped, like so:
"c:\\temp"
However, with a string literal, there are no escape characters, so you
don''t have to worry. You can write this:
@"c:\temp"
2) Is there a performance loss if I start concatenating strings together
with the + operator ? In Java, they discouraged code like this:
string command = "CMD " + var1 + " " + var2;
The reason is because of all of the temporary String objects that get
created in the meantime. The solution is to use a StringBuffer like so:
StringBuffer command = new StringBuffer("CMD ");
command.append(var1);
command.append('' '');
command.append(var2);
System.out.println(command.toString());
This is less expensive than the above code because String objects are not
being created left & right. Does C# have the same problem? I noticed that C# has a StringBuilder class which is just like the Java StringBuffer class. Should I use StringBuffer in cases like the above where I need to
concatenate multiple strings to one string?
Generally speaking, if you have a lot of string manipulation to do, then
you should use a StringBuilder instance to do it. It will be faster than
just concatenating strings. It should be noted that the following line:
string command = "CMD " + var1 + " " + var2;
Is actually going to be compiled into:
string command = String.Concat("CMD ", var1, " ", var2);
Underneath the covers, Concat uses a StringBuilder instance to do its
work. Also, it should be noted that the compiler treats the concatenation
of two string literals as a single instance. In other words:
"Hello " + "there"
Is compiled into:
"Hello there"
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
这篇关于C#字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!