What is the difference between a Character Array and a String?

Todd Moses picture Todd Moses · Feb 17, 2010 · Viewed 70.6k times · Source

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?

Answer

Draemon picture Draemon · Feb 17, 2010

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.