字符数组和字符串有什么区别?

发布于 2024-08-21 18:09:04 字数 82 浏览 8 评论 0原文

将时间花在高级语言上时,我突然意识到我不知道字符数组和字符串之间的区别。我认为它们是同一件事,但不确定。有区别吗?它只是一个带有一些抽象的字符数组吗?

Spending my time on high level languages it suddenly occurred to me that I did not know the difference between a Character Array and a String. I think they are the same thing but not sure. Is there a difference? Is it just a Character Array with some abstraction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

想挽留 2024-08-28 18:09:04

字符串的一种抽象,但是是字符序列的抽象。它没有提到实施。如果您想创建一个基于字符链接列表的 String 实现,那么没有什么可以阻止您。

在诸如 C 之类的语言中,差别很小 - 只是 c 字符串是位于连续地址的一系列以 null 结尾的字符,通常通过指针进行访问。

在 OOP 语言中,String 将是某个 String 类的对象。这可能会在内部将数据保存在字符数组中,但您不需要知道这一点。字符数组只能是一个简单的数组,但如果实现者决定的话,String 类可以提供许多对字符串的操作(子字符串、正则表达式等)。

A String is an abstraction, but of a sequence of characters. It says nothing of implementation. If you wanted to create a String implementation based on a linked list of characters there's nothing stopping you.

In a language such as C, there is very little difference - just that a c string is a null-terminated series of characters at sequential addresses, that is generally accessed through a pointer.

In an OOP language, a String will be an object of some String class. This will probably hold the data in a character array internally, but you don't need to know that. A character array can only be a simple array, but a String class can provide many operations (substrings, regex, etc) on strings if the implementer decides to.

筑梦 2024-08-28 18:09:04

字符数组只是一个字符数组

字符串是使用字符数组的数据结构 某些字符串表示形式

使用空终止符(如 C),其他字符串表示形式使用长度前缀

a character array is simply an array of characters

a string is data structure that uses an array of characters

some string representations use a null-terminator (like C), others use a length prefix

向地狱狂奔 2024-08-28 18:09:04

我曾经教过编程,这就是我用来解释这个特定问题的方式。

首先,关注两者的共同点:字符数组和字符串都由字符序列组成。例如,作为一个序列意味着字符是有序的并且它们可以被枚举。

现在,重点关注这两件事中的每一个,以它们特定的不同方式,为这个共同点添加了什么。

字符数组添加了任何已知数组都会添加的内容:索引和对单个项目的随机访问。

另一方面,字符串增加了这样一个事实:字符序列被视为具有自己属性的整体。在某些实现中,实现这一点意味着改变字符的存储方式(例如,在 C 字符串中添加终止 null)。

这种方法(看看共性,然后看看事物与它们的差异)在各种情况下都被证明是有用的。

希望这有帮助。

I used to teach programming, and this is how I used to explain this particular issue.

First, focus on what both things have in common: both a char array and a string consist of a sequence of characters. Being a sequence implies that the characters are ordered and that they can be enumerated, for example.

Now focus on what each of the two things add, in their particular different ways, to this common ground.

A char array adds what any array is known to add: indexing and random access to individual items.

A string, on the other hand, adds the fact that the sequence of chars is seen as a whole thing with its own properties. In some implementations, achieving this means altering the way the chars are stored (adding a terminating null in C strings, for example).

This approach (look at the commonalities, then at how things diverge from them) has proven useful in a variety of situations.

Hope this helps.

临走之时 2024-08-28 18:09:04

在 C 中,这些几乎是相同的,尽管字符串末尾会有一个额外的空字符。

在其他语言(Java、C# 等)中,字符串是对象,而字符数组是...字符(基本数据类型)的数组。

通常,字符串是用字符数组实现的。

In C these are almost the same, though a string will have an additional null character at the end.

In other languages (Java, C# etc), a string is an object, whereas a character array is an array of ... chars (which are primitive data types).

Normally, strings are implemented with character arrays.

分開簡單 2024-08-28 18:09:04

答案在某种程度上取决于您谈论的语言。在 .Net/C# 世界中,字符串是不可变的对象,而 char 数组可以在数组中轻松添加/更改值。字符串可以以只读方式视为字符数组,因为您可以迭代字符串中的字符。

简而言之,我认为最大的区别在于你想如何与他们合作。您是否想要处理一大段文本,例如向最终用户显示消息,或者您是否正在查看字符序列,对列表进行一些处理?在某种程度上,这都是相当主观的。

The answer to some extent depends on what language your talking about. In the .Net/C# world strings are immutable objects, whereas a char array you can add/change values easily in the array. Strings can be treated as char arrays in a read-only fashion, as you can iterate over the characters in a string.

In the abstract I think the biggest difference is in how you want to work with them. Are you wanting to work with a chunk of text, say to show a message to an end user, or are you looking at a sequence of characters, doing some processing on the list? It's all rather subjective at a certain level.

胡大本事 2024-08-28 18:09:04

这取决于语言。在 C 语言中,它们几乎是同义词。你可以声称区别在于“字符串”有一个隐式的终止 nul,但这会让人吹毛求疵。

Fortran 是另一个极端。字符数组和字符串是完全不同的类型,可以进行不同的操作。

It depends on the language. In C-ish languages, they are pretty much synonomous. You could claim the difference is that "strings" have an implicit terminating nul, but that would be splitting hairs.

Fortran is the other extreme. There character arrays and character strings are entirely different types, with different operations available for them.

夏日落 2024-08-28 18:09:04

String是java中的一个。所以它有属性,例如length。因此,当您询问字符串的大小时,它只是返回该值,而不是每次都计算该值。它还提供其他方法,例如 indexOfsubstring 等,让生活变得轻松,这样您就不必自己做这些。

String is a class in java. So it has attributes e.g. length. So when you ask for the size of string it simply returns that instead of computing the value each time. It also other methods e.g. indexOf, substring, etc to make life easy so you don't have to do that yourself.

念﹏祤嫣 2024-08-28 18:09:04

C 风格的字符串在内部由字符数组表示,末尾带有“\0”,表示字符串的结束。

在C++中,string.h中定义了一个字符串容器类,它提供了一些典型的字符串操作来操作字符串。

A C-style string is internally represented by array of character with the '\0' at end, which indiciates the end of string.

In C++, there's a string container class defined in string.h which provides some typical string operations to manipulate the string.

舂唻埖巳落 2024-08-28 18:09:04

字符串是以空字符“\0”结尾的字符数组

A string is the character array terminated by null character ‘\0’

待"谢繁草 2024-08-28 18:09:04

在C中,字符串是一个以空字符(\0)结尾的字符数组,但

在C++中,字符串是一个类,我们使用它的对象,并且末尾没有空字符
但字符数组末尾包含空字符。

另外,我们可以在 C++ 中对字符串对象使用运算符。

In C, a string is an array of characters terminated by a null character(\0) but

In C++, a string is a class and we use its object and there is no null character at the end
but an array of characters contains null character at the end.

Also, we can use operators with the string object in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文