简体   繁体   English

C#中双引号和单引号之间的区别是什么

[英]What's the difference between double quotes and single quote in C#

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#中双引号和单引号之间的区别是什么?

Single quotes encode a single character (data type char ), while double quotes encode a string of multiple characters. 单引号进行编码的单个字符(数据类型char ),而双引号进行编码的多个字符的字符串。 The difference is similar to the difference between a single integer and an array of integers. 差异类似于单个整数和整数数组之间的差异。

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];

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]是该字符串中特定索引处的char(在这种情况下为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. 正如您所看到的,使用单引号来确定单个char比尝试将字符串转换为char以进行测试要容易得多。

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! 希望我没有更多的混淆你!

单引号表示单个字符'A' ,双引号将空终结符'\\0'附加到字符串文字的末尾, " "实际上是" \\0" ,它比预期大小大一个字节。

Single quotes instead of double ones? 单引号而不是双引号?

Where? 哪里? Here? 这里? if(text[i] == " ") if(text [i] ==“”)

text[i] gives a character/byte and this is compared to an array of (probably unicoded ??) characters/bytes. text [i]给出一个字符/字节,并将其与一个(可能是unicoded ??)字符/字节数组进行比较。 That does not work well. 这不太好用。

Say: compare '1' with 1 说:比较'1'和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”与“一”或(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 ? 这里有一个单词或2个单词或更多单词?

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) (顺便说一下,如果文件包含带点的句子。有人忘了在点后面添加一个空格,这个单词将不会添加到单词总数中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM