简体   繁体   English

如何在C#和Javascript中处理字符串VS字符?

[英]How are strings VS chars handled in C# vs Javascript?

In JavaScript, single and double quotes are somewhat interchangeable and largely a matter of styles (There is a good discussion of why this isn't actually the case in one of the answers here: When to use double or single quotes in JavaScript? ). 在JavaScript中,单引号和双引号在某种程度上是可以互换的,并且很大程度上是样式的问题(这里有一个很好的讨论,为什么在这里的答案之一实际上并非如此: 何时在JavaScript中使用双引号或单引号? )。 How are chars and strings handled in C#? 如何在C#中处理字符和字符串?

For example: 例如:

string test = "hello world";
string test2 = 'hello world'; // Too many characters in character literal
char test3 = 'a';
char test4 = "a"; // Cannot implicitly convert type string to char

It looks like strings and chars are being handled as separate, interchangeable types, and that the use of single or double quotes demarcates this? 它看起来像字符串和字符作为单独的,可互换的类型处理,并且单引号或双引号的使用划分了这个?

What is the relationship between chars and strings in typed languages? 类型语言中字符和字符串之间的关系是什么? Specifically, would it be correct to say that a string is an array of chars? 具体来说,说一个字符串是一个字符数组是否正确?

would it be correct to say that a string is an array of chars 说一个字符串是一个字符数组是否正确

In .NET, a string is an object containing a contiguous block of memory containing UTF-16 code units. 在.NET中,字符串是一个对象,包含一个包含UTF-16代码单元的连续内存块。 A char is another (primitive) data type that just contains one code point, with no object overhead. char是另一个(原始)数据类型,它只包含一个代码点,没有对象开销。

From this interesting blog post from Jon Skeet , where he compares the .NET vs. Java implementation: Jon Skeet的这篇有趣的博客文章中 ,他比较了.NET与Java的实现:

A long string consists of a single large object in memory. 长字符串由内存中的单个大对象组成。 Compare this with Java, where a String is a “normal” type in terms of memory consumption, containing an offset and length into a char array – so a long string consists of a small object referring to a large char array. 将其与Java进行比较,其中String在内存消耗方面是“普通”类型,包含char数组的偏移量和长度 - 因此长字符串由引用大型char数组的小对象组成。

C# uses the quotes to indicate the type - ' is always a char , " is always a string . C#使用引号指示类型- '始终是一个char"始终是一个string

In .NET a string behaves like a read-only array of char , so: 在.NET中,字符串的行为类似于char的只读数组,因此:

// Treat string like an array
char c = "This is a string"[3];
int len = "This is a string".Length

// Now we have the char at pos 3
c == 's';

What you can't do is edit them: 你不能做的就是编辑它们:

// This fails
"This is a string"[3] = 'x';

// This is fine, because we get a new editable char[]
char[] c = "This is a string".ToCharArray();
c[3] = 'x';

This is true in any .NET language as they all use the same string implementation. 在任何.NET语言中都是如此,因为它们都使用相同的字符串实现。 Other strongly typed frameworks have different ways of handling strings. 其他强类型框架有不同的处理字符串的方法。

In .NET char can be explicitly cast to an int and implicitly cast back, so: 在.NET中, char可以显式地转换为int并隐式转换,因此:

char c = (char) 115; // 's'
int i = c; // 115

Finally char is a value type, while string (being a collection of character bytes under the covers) is actually an immutable reference type. 最后char是一个值类型,而string (作为封面下字符字节的集合)实际上是一个不可变的引用类型。 These behave very similarly in code but are stored in different ways in memory - I think this is why C# makes the distinction in how they are delimited - "s" and 's' are different things and stored in different ways (entirely unlike the Javascript string ). 这些在代码中的行为非常相似,但在内存中以不同的方式存储 - 我认为这就是为什么C#区分它们如何分隔 - "s"'s'是不同的东西并以不同的方式存储(完全不同于Javascript string )。

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

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