Reference.String History

Hide minor edits - Show changes to output

August 01, 2010, at 02:16 PM by Tom Igoe -
Changed lines 5-6 from:
Strings are represented as arrays of type char and are null-terminated.
to:
Text strings can be represented in two ways. you can use the String data type, which is part of the core as of version 0019, or you can make a string out of an array of type char and null-terminate it. This page described the latter method. For more details on the String object, which gives you more functionality at the cost of more memory, see the [[StringObject|String object]] page.
July 24, 2009, at 01:31 AM by Paul Badger -
June 25, 2009, at 01:07 PM by Paul Badger -
Changed lines 75-80 from:





to:
* [[http://arduino.cc/en/Reference/VariableDeclaration|Variable Declaration]]





January 11, 2008, at 05:52 AM by Paul Badger -
Changed lines 50-51 from:
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.
to:
It is often convenient, when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.
November 18, 2007, at 06:14 AM by Paul Badger -
November 18, 2007, at 06:11 AM by Paul Badger -
Changed lines 29-32 from:
Generally, strings are terminated with a null character (ASCII code 0). This allows function (like Serial.print()) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.

This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
to:
Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like Serial.print()) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.

This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled with a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
November 05, 2007, at 10:02 PM by Paul Badger -
Changed lines 50-53 from:
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an expample of a two-dimensional array.

It isn't necessary to understand this construction in detail to use it effectively. The code fragments below illustrate the idea.
to:
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.

In the code below, the asterisk after the datatype char "char*" indicates that this is an array of "pointers". All array names are actually pointers, so this is required to make an array of arrays. Pointers are one of the more esoteric parts of C for beginners to understand, but it isn't necessary to understand pointers in detail to use them effectively here.

!!!!Example
October 28, 2007, at 03:58 AM by Paul Badger -
Changed lines 72-79 from:
[[array]]
[[PROGMEM]]





to:
*[[array]]
*[[PROGMEM]]





October 28, 2007, at 03:58 AM by Paul Badger -
Changed lines 70-76 from:






to:
!!!!See Also

[[array]]
[[PROGMEM]]





October 28, 2007, at 03:50 AM by Paul Badger -
Changed line 65 from:
Serial.print(myStrings[i]);
to:
Serial.println(myStrings[i]);
October 28, 2007, at 03:36 AM by Paul Badger -
October 28, 2007, at 03:29 AM by Paul Badger -
Changed lines 50-53 from:
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array.

It isn't necessary to understand this contruction in detail to use it effectively. The code fragments below illustrate the idea.
to:
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an expample of a two-dimensional array.

It isn't necessary to understand this construction in detail to use it effectively. The code fragments below illustrate the idea.
October 28, 2007, at 03:26 AM by Paul Badger -
Deleted line 63:
October 28, 2007, at 03:26 AM by Paul Badger -
Changed line 56 from:
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",//
to:
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",
October 28, 2007, at 03:25 AM by Paul Badger -
Changed lines 56-59 from:
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",\\
"This is string 4", "This is string 5","This is string 6",
};
to:
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",//
"This is string 4", "This is string 5","This is string 6"};
October 28, 2007, at 03:24 AM by Paul Badger -
Changed lines 50-51 from:
It is often useful when working with several strings, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array. The code fragments below illustrate the idea.
to:
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array.

It isn't necessary to understand this contruction in detail to use it effectively. The code fragments below illustrate the idea.
Changed lines 56-57 from:
char* myStrings[]={"This is string 1", "This is string 2"};
to:
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",\\
"This is string 4", "This is string 5","This is string 6",
};
Changed lines 65-68 from:
Serial.print(myStrings[0]);
delay(100);
Serial.print(myStrings[1]);
delay(100);
to:

for (int i = 0; i < 6; i++){
Serial.print(myStrings[i]);
delay(500);
}
October 28, 2007, at 03:16 AM by Paul Badger -
Changed lines 18-19 from:
''Possibilities for declaring strings''
to:
'''Possibilities for declaring strings'''
Changed lines 35-36 from:
''Single quotes or double quotes?''
to:
'''Single quotes or double quotes?'''
Changed lines 39-40 from:
''Wrapping long strings''
to:
'''Wrapping long strings'''
Changed lines 48-53 from:





to:
'''Arrays of strings'''

It is often useful when working with several strings, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array. The code fragments below illustrate the idea.

[@

char* myStrings[]={"This is string 1", "This is string 2"};

void setup(){
Serial.begin(9600);
}

void loop(){
Serial.print(myStrings[0]);
delay(100);
Serial.print(myStrings[1]);
delay(100);
}@]







October 28, 2007, at 03:09 AM by Paul Badger -
Changed lines 27-28 from:
''Null termination''
to:
'''Null termination'''
September 27, 2007, at 07:34 PM by David A. Mellis -
Changed lines 5-6 from:
Strings in the C programming language are represented as arrays of type char.
to:
Strings are represented as arrays of type char and are null-terminated.
September 27, 2007, at 05:50 PM by Paul Badger -
Changed lines 5-6 from:
Strings in the C programming language are represented as arrays of chars.
to:
Strings in the C programming language are represented as arrays of type char.
September 27, 2007, at 05:47 PM by Paul Badger -
Changed lines 31-32 from:
This means that your string needs to have space for more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
to:
This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
May 31, 2007, at 04:30 PM by David A. Mellis -
Changed line 7 from:
!!!!String declarations
to:
!!!!Examples
May 31, 2007, at 04:29 PM by David A. Mellis - Clarifying the language.
Changed lines 5-6 from:
Strings in the C programming language are defined as arrays of type char.
to:
Strings in the C programming language are represented as arrays of chars.
Changed line 12 from:
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
to:
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
Changed lines 23-24 from:
* Initialization is in the form of a string constant in quotation marks, the compiler will size the array to fit the constant and add the extra null, Str4
* Initialize the array with an explicit size, Str5
to:
* Initialize with a string constant in quotation marks; the compiler will size the array to fit the string constant and a terminating null character, Str4
* Initialize the array with an explicit size and string constant, Str5
Deleted line 26:
Changed lines 29-32 from:
Note the differences between Str2 & Str3, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2.

When declaring a string, you must declare an extra character for this null or the compiler will complain with an error about the initialization string being too long. For the same reasons, Str3 can hold only 14 characters, not 15, as one might assume.
to:
Generally, strings are terminated with a null character (ASCII code 0). This allows function (like Serial.print()) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.

This means that your string needs to have space for more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.

Note that it's possible to have a string without a final null character (e.g. if you had specified the length of Str2 as seven instead of eight). This will break most functions that use strings, so you shouldn't do it intentionally. If you notice something behaving strangely (operating on characters not in the string), however, this could be the problem.
May 31, 2007, at 04:16 PM by David A. Mellis - don't want {} around a multi-line string - it makes it into an array of strings.
Changed line 42 from:
char myString[] = {"This is the first line"
to:
char myString[] = "This is the first line"
Changed line 44 from:
" etcetera"};
to:
" etcetera";
May 29, 2007, at 09:16 PM by Paul Badger -
Changed lines 38-39 from:

to:
''Wrapping long strings''

You can wrap long strings like this:
[@
char myString[] = {"This is the first line"
" this is the second line"
" etcetera"};
@]






April 16, 2007, at 06:03 PM by Paul Badger -
Changed line 39 from:
[[HomePage | Reference Home]]
to:
April 15, 2007, at 10:00 PM by Paul Badger -
Changed lines 21-27 from:
* Declare an array of chars (with one extra char)and the compiler will add the required null character
* Explicitly add the null character
* Initialization is in the form of a string constant in quotation marks, the compiler will size the array to fit the constant and add the extra null
* Initialize the array with an explicit size
* Initialize the array, leaving extra space for a larger string

to:
* Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2
* Explicitly add the null character, Str3
* Initialization is in the form of a string constant in quotation marks, the compiler will size the array to fit the constant and add the extra null, Str4
* Initialize the array with an explicit size, Str5
* Initialize the array, leaving extra space for a larger string, Str6

April 15, 2007, at 07:54 PM by Paul Badger -
Changed line 8 from:
All of the following are valid declarations of valid strings.
to:
All of the following are valid declarations for strings.
Changed lines 17-18 from:
'''Facts about strings'''
to:
April 15, 2007, at 07:53 PM by Paul Badger -
Changed line 8 from:
Strings can be declared in any of the following manners
to:
All of the following are valid declarations of valid strings.
Changed lines 10-12 from:
char Str1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char Str3[4];
to:
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
Changed lines 17-56 from:
!!!!Null termination
Note the differences between Str1 & Str2, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2. If you haven't declared an extra character for this null, the compiler will complain with an error about the initialization string being too long.



In the first example the array is initialized with constants of type '''char''', 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing them in single quotes.
In example four, the initialization of the string is shown with the whole word in '''quotation marks''' as in "arduino"


Note that in example 4 above, the

!!!!Example

[@
long time;

void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
}
@]
!!!!Parameters

[=long var = val;=]

*var - your long variable name
*val - the value you assign to that variable





to:
'''Facts about strings'''

''Possibilities for declaring strings''

* Declare an array of chars without initializing it as in Str1
* Declare an array of chars (with one extra char)and the compiler will add the required null character
* Explicitly add the null character
* Initialization is in the form of a string constant in quotation marks, the compiler will size the array to fit the constant and add the extra null
* Initialize the array with an explicit size
* Initialize the array, leaving extra space for a larger string


''Null termination''

Note the differences between Str2 & Str3, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2.

When declaring a string, you must declare an extra character for this null or the compiler will complain with an error about the initialization string being too long. For the same reasons, Str3 can hold only 14 characters, not 15, as one might assume.

''Single quotes or double quotes?''

Strings are always defined inside double quotes ("Abc") and characters are always defined inside single quotes('A').

April 15, 2007, at 06:24 AM by Paul Badger -
April 15, 2007, at 06:24 AM by Paul Badger -
Changed lines 10-15 from:
char myStr1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char myStr2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr3[4];
char myStr4[ ] = "arduino";
char myStr5[8] = "arduino";
char myStr6[15] = "arduino";
to:
char Str1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char Str3[4];
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
Changed lines 17-18 from:

A couple of things to note.
to:
!!!!Null termination
Note the differences between Str1 & Str2, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2. If you haven't declared an extra character for this null, the compiler will complain with an error about the initialization string being too long.


April 15, 2007, at 06:12 AM by Paul Badger -
Changed lines 10-15 from:
char myStr[7] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char myStr[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr[4];
char myStr[ ] = "arduino";
char myStr[7] = "arduino";
char myStr[15] = "arduino";
to:
char myStr1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char myStr2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr3[4];
char myStr4[ ] = "arduino";
char myStr5[8] = "arduino";
char myStr6[15] = "arduino";
Changed lines 19-22 from:
In the first example the array is initialized with constants of type '''char''' 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing in single quotes.
In example four, the initialization of the string is shown with the whole word in '''quotation marks'''

to:
In the first example the array is initialized with constants of type '''char''', 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing them in single quotes.
In example four, the initialization of the string is shown with the whole word in '''quotation marks''' as in "arduino"

April 15, 2007, at 06:07 AM by Paul Badger -
Changed lines 10-15 from:
char str[5] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char str[6] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char str[3];
char str[ ] = "arduino";
char str[7] = "arduino";
char str[15] = "arduino";
to:
char myStr[7] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char myStr[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr[4];
char myStr[ ] = "arduino";
char myStr[7] = "arduino";
char myStr[15] = "arduino";
Changed lines 19-21 from:
In the first example the array is initialized with constants of type '''char'''.

to:
In the first example the array is initialized with constants of type '''char''' 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing in single quotes.
In example four, the initialization of the string is shown with the whole word in '''quotation marks'''

April 15, 2007, at 06:02 AM by Paul Badger -
Added lines 1-53:
!!string

!!!!Description

Strings in the C programming language are defined as arrays of type char.

!!!!String declarations
Strings can be declared in any of the following manners
[@
char str[5] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char str[6] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char str[3];
char str[ ] = "arduino";
char str[7] = "arduino";
char str[15] = "arduino";
@]

A couple of things to note.
In the first example the array is initialized with constants of type '''char'''.


Note that in example 4 above, the

!!!!Example

[@
long time;

void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
}
@]
!!!!Parameters

[=long var = val;=]

*var - your long variable name
*val - the value you assign to that variable






[[HomePage | Reference Home]]

Share