tags:

views:

415

answers:

9

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..."?

+1  A: 

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

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

Daok
Damn you, 44 seconds! =P
Erik Forbes
What kind of definition uses the term being defined in the definition itself? :)
Rob Hruska
The first string is the name of keyword, the second means "string" as a general term :)
Mehrdad Afshari
Microsoft definition, I think they mean The class named "string" represent a string (character) of unicode... ;)
Daok
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
Got down voted twice here hummm it's the official answer from Microsoft :P
Daok
Doak, Probably people hate MS too much! By the way, it's the official definition of string type, not the answer to the question. :))
Mehrdad Afshari
What are strings really? it's a string of unicode character.
Daok
:)) Lol. Is string, string?
Mehrdad Afshari
+3  A: 

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."

Aidan Ryan
A: 

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.

shahkalpesh
+8  A: 

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. :))

Mehrdad Afshari
A: 

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.

Steven Behnke
A: 

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.

Nick
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)...
Charles Bretana
+6  A: 

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.

jalf
A: 

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).

ctacke
A: 

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.

comingstorm