C#中双引号和单引号有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between double quotes and single quote in C#
提问by
What's the difference between double quotes and single quote in C#?
C#中的双引号和单引号有什么区别?
I coded a program to count how many words are in a file
我编写了一个程序来计算文件中有多少单词
using System;
using System.IO;
namespace Consoleapp05
{
class Program
{
public static void Main(string[] args)
{
StreamReader sr = new StreamReader(@"C:\words.txt");
string text = sr.ReadToEnd();
int howmany = 0;
int howmany2 = 0;
for(int i = 0; i < text.Length; i++)
{
if(text[i] == " ")
{
howmany++;
}
}
howmany2 = howmany + 1;
Console.WriteLine("It is {0} words in the file", howmany2);
Console.ReadKey(true);
}
}
}
This gives me an error because of the double quotes. My teacher told me to use single quote instead, but he didn't tell me why. So what's the difference between double quotes and single quote in C#?
由于双引号,这给了我一个错误。我的老师告诉我改用单引号,但他没有告诉我为什么。那么C#中的双引号和单引号有什么区别呢?
回答by Konrad Rudolph
Single quotes encode a single character (data type char), while double quotes encode a string of multiple characters. The difference is similar to the difference between a single integer and an array of integers.
单引号编码单个字符(数据类型char),而双引号编码多个字符的字符串。区别类似于单个整数和整数数组之间的区别。
char c = 'c';
string s = "s"; // String containing a single character.
System.Diagnostics.Debug.Assert(s.Length == 1);
char d = s[0];
int i = 42;
int[] a = new int[] { 42 }; // Array containing a single int.
System.Diagnostics.Debug.Assert(a.Length == 1);
int j = a[0];
回答by jrEving
Single quotes instead of double ones?
单引号而不是双引号?
Where? Here? if(text[i] == " ")
在哪里?这里?如果(文本[i] ==“”)
text[i] gives a character/byte and this is compared to an array of (probably unicoded ??) characters/bytes. That does not work well.
text[i] 给出一个字符/字节,并将其与(可能是 unicode ??)字符/字节数组进行比较。那效果不好。
Say: compare '1' with 1
or "1" with "one"
or (2-1) with "eins"
what do you think are the correct answers, or is there no meaningful answer anyway?
说:将“1”与 1
或“1”与“一”或 (2-1) 与“eins”进行比较,您认为正确答案是什么,还是没有有意义的答案?
Besides that: the program will not work very well with single quotes either, given the example "words.txt" =
除此之外:该程序也不能很好地使用单引号,例如“words.txt”=
one word or 2 words or more words here ?
这里是一个词还是两个词或更多词?
回答by RvdK
you are looking for spaces, this can be done as a space in a string or as a char. So in my opinion this would work.
您正在寻找空格,这可以作为字符串中的空格或作为字符来完成。所以在我看来这会奏效。
(By the way, if the file contains sentences with dots. And someone forgot to add a space after the dot, the word will not be added to the total amount of words)
(顺便说一句,如果文件中包含带点的句子。而有人忘记在点后加一个空格,该词将不会被添加到总词数中)
回答by Wayne
when you say string s = "this string" then s[0] is a char at at specific index in that string (in this case s[0] == 't')
当您说 string s = "this string" 时, s[0] 是该字符串中特定索引处的字符(在本例中为 s[0] == 't')
So to answer your question, use double quotes or single quotes, you can think of the following as meaning the same thing:
因此,要回答您的问题,请使用双引号或单引号,您可以将以下内容视为同一件事:
string s = " word word";
// check for space as first character using single quotes
if(s[0] == ' ') {
// do something
}
// check for space using string notation
if(s[0] == " "[0]) {
// do something
}
As you can see, using a single quote to determine a single char is a lot easier than trying to convert our string into a char just for testing.
如您所见,使用单引号确定单个字符比尝试将我们的字符串转换为仅用于测试的字符要容易得多。
if(s[0] == " "[0]) {
// do something
}
is really like saying:
真的就像在说:
string space = " ";
if(s[0] == space[0]) {
// do something
}
Hopefully I did not confuse you more!
希望我没有让你更加困惑!
回答by skouliou
the single quote represent a single character 'A', double quote append a null terminator '\0'to the end of the string literal, " "is actually " \0"which is one byte larger than the intended size.
单引号表示单个字符'A',双引号'\0'在字符串文字的末尾附加一个空终止符," "实际上" \0"比预期的大小大一个字节。

