Strings

The technical description of a String is: an array of characters. The informal view of a string is a sentence. Strings are almost always written in code as a quoted sequence of characters, i.e., "this is a string".

Strings

Strings are like sentences. They are formed by a list of characters, which is really an "array of characters". Strings are very useful when communicating information from the program to the user of the program. They are less useful when storing information for the computer to use.

Here are some examples of creating strings in various languages.

Matlab

            
            message_to_user = 'hello, this is the computer';    % notice the use of single quotes!!!
            
          

Notice the use of the single quote "'" to start and stop the string. Most languages use double quotes, but Matlab uses the single quote.

ActionScript

            
            var  message_to_user : String;
            message_to_user = "hello, this is the computer";
            
          

C language

In C we have to predefine the length of the string before we create it. Thus the maximum length of the string is defined before we know what we will put in it. This means that some of the characters in the array will not be used! To denote the un-used part of the string (at the end of the array), C puts an invisible "null" character to indicate the end of the string. The null character is written: '\0'.

Additionally in C, we cannot use the equal sign to assign a string to an array; we must use the function sprintf, as shown below:

            
            char message_to_user[100];
            sprintf(message_to_user, "hello, this is the computer");
            
          

sprintf automatically puts the null character after the 'r' in computer, indicating the end of the string.


Concatenation ("adding" strings together)

It is possible to "add" strings together, which really means concatenation. Every programming language provides this ability in either an easier or more difficult manner. We'll start from easy to hard.

Matlab

Matlab treats all strings as arrays, and you can concatenate any array to another by listing them both inside [] with a comma separating them, thus:

            
              greeting = 'hello';
              user     = 'jim';
              
              message = [greeting, ' to you ', user];
            
          

ActionScript

To concatenate two strings we do in fact "add" them together. Even more nicely, ActionScript allows us to add numbers to a string to create a new string. See the examples below.

            
            var  message_to_user : String;
            message_to_user = "hello, this is the computer";

            message_to_user = "hello" + " " + "how are you";

            var grade : int = 100;
            var name  : String = "jim";

            message_to_user = "Hello " + name + ", your grade is: " + grade;
            
          

By using the concatenation operator (the plus) we can quickly cobble together useful messages for the user to read, containing multiple pieces of information.

C Language

C does not allow the '+' operator to concatenate. One way to achieve concatenation is using sprintf.

            
              char long_string[1000];
              char string1[100];
              char string2[100];
              sprintf(long_string, "%s%s", string1, string2);

              // here we concatenate string1 and string2 and store
              // then in long_string
            
          

Converting other Data types to Strings

Depending on the language, you can more or less easily convert data into a string.

Matlab

In Matlab, you have to use the sprintf command (using a format string).

ActionScript

In ActionScript, all classes have a "toString" method which returns a string representation of the information in the class. See toString.

You can call the toString explicitly or you can use the following "trick", which creates a string expression by using the empty string "" and the concatenation operation:

            
              "" + 5    // creates the string "5"
              "" + true // creates the string "true"
              "" + null // creates the string "null"
            
          

C Language

In C you will have to use the sprintf command (using a format string).


Back to Topics List