5

Is a string actually a character array (is-a), or does it have a character array as an internal store (has-a), or is it's own object which can expose itself as a with an array of characters?

I am more inclined to say it is it's own object, but then why are we so inclined to always say "A string is an array of characters..."?

3
  • Why do you ask? Curiosity? Some problem your facing? Commented Dec 10, 2008 at 19:56
  • A magical bit of cyber-twine dipped in a gooy array of characters.
    – Echostorm
    Commented Dec 10, 2008 at 20:03
  • Please edit the question to mention .NET somewhere in it. I answered based on the question tag. Commented Dec 10, 2008 at 20:11

9 Answers 9

7

the .NET string is not just an array of characters. It contains an array of characters, so strictly speaking, it's has-a.

Moreover, there are a lot of Unicode-related subtleties where it doesn't behave anything like an array. Concatenating a character may do a lot more than just increase the string length by one, and insert the new character at the end. According to the Unicode normalization rules, it may actually change the entire string. So it is definitely nothing like an array of characters, but somewhere within the class, such an array exists.

7

It depends on your definition of the word "string".

System.String type in .NET has a character array as internal store (it also stores length (which is O(1)), among other things, for example).

But the word string means a consecutive occurrence of something in general, which might also mean a character array :))

By the way, when I said string type has a "character array," I didn't mean "a field of type char[]" specifically. I meant the general meaning of the term "array" as an ordered collection of something. :))

3

The semantic meaning of string is the second.

The .Net String class maintains an internal store (has-a) and can expose that store in as many abstract ways as the designers choose.

The question is like "Is an Apple a red round thing? Because I always thought it was a fruit."

1

MSDN : The string type represents a string of Unicode characters.

This mean : it's an array of Unicode character.

6
  • What kind of definition uses the term being defined in the definition itself? :)
    – Rob Hruska
    Commented Dec 10, 2008 at 20:02
  • The first string is the name of keyword, the second means "string" as a general term :) Commented Dec 10, 2008 at 20:03
  • Microsoft definition, I think they mean The class named "string" represent a string (character) of unicode... ;) Commented Dec 10, 2008 at 20:03
  • I suppose I can accept that as an answer. I will also end this comment with a smiley face, because apparently that's what we're supposed to do? :)
    – Rob Hruska
    Commented Dec 10, 2008 at 20:05
  • Got down voted twice here hummm it's the official answer from Microsoft :P Commented Dec 10, 2008 at 20:08
0

In an abstract way (and I guess, when laid out in memory) - it is an array of characters.

Correct me, if I am wrong in thinking that.

0

String is a name of a class. It has different meanings in different languages. It could be unicode or ASCII internally meaning it's storage mechanism is a series of bytes. This class provides functions for manipulating it's own internal storage and it is not meant to be directly accessed and modified due to the fact that it could contain characters in a variety of different encodings. So for the purpose of your question, it has-a byte store.

0

It depends on the language and implementation. On a most basic level (ascii char* string) it is a sequential series of memory addresses each of which contain a short int corresponding to a ascii code and terminated by null (char(0)). Most higher level languages provide a string object which has a character array as well as convenience methods because working with char* strings is more or less a pain in the rear.

1
  • If you read the question and actually mean that it is dependant on which ".Net" language, then you are wrong... it does not... All .net strings regardless of language are the same, as defined by the .Net Common Type System (CTS)... Commented Dec 10, 2008 at 20:30
0

Depends on exactly how you look at it. If you pin it with a GCHandle and then look at the memory where it resides, you're see it's actually a 32-bit length descriptor followed immediately by an array of unicode characters (be aware that AddrOfPinnedObject will give you the address of the first character, not the length. If it gave the address of the length it would be far less useful for P/Invoking).

0

Functionally, a string is a list, or sequence, of characters. Strings are often stored transparently as character arrays (e.g., in C), so we often refer to them that way. Arrays allow convenient random access to the characters, which is important for some algorithms.

For other purposes, though, storing Unicode strings as UTF-8 might be the most appropriate form. Note that, although it's stored in a byte array, there's no longer a one-to-one correspondence between bytes and characters: your string algorithms generally need to access the characters sequentially from the beginning -- as a list.

The moral of this story is: your string code should only demand random access if it actually needs it. You might be surprised how seldom you really need an array of characters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.