1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-18 07:52:14 +01:00

added more String examples

This commit is contained in:
Tom Igoe 2010-07-27 22:59:18 +00:00
parent 4df6fde6b5
commit 81e7814887
13 changed files with 392 additions and 54 deletions

View File

@ -4,13 +4,10 @@
Examples of how to add strings together
You can also add several different data types to string, as shown here:
The circuit: No external hardware needed.
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
// declare three strings:

View File

@ -1,41 +0,0 @@
Arduino is an open-source physical computing platform based on a simple i/o
board and a development environment that implements the Processing/Wiring
language. Arduino can be used to develop stand-alone interactive objects or
can be connected to software on your computer (e.g. Flash, Processing, MaxMSP).
The boards can be assembled by hand or purchased preassembled; the open-source
IDE can be downloaded for free.
For more information, see the website at: http://www.arduino.cc/
or the forums at: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl
To report a bug or a make a suggestions, go to:
[hardware] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=hwbugs
[software] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs
INSTALLATION
Detailed instructions are in reference/Guide_Windows.html and
reference/Guide_MacOSX.html. For Linux, see the Arduino playground:
http://www.arduino.cc/playground/Learning/Linux
If you are using a USB Arduino, you will need to install the drivers for the
FTDI chip on the board. These can be found in the drivers/ directory.
* On Windows, plug in the Arduino board and point the Windows Add Hardware
wizard to the drivers/FTDI USB Drivers sub-directory of the Arduino
application directory.
* On the Mac, install the FTDIUSBSerialDriver_10_4_10_5_10_6.mpkg package.
* On Linux, drivers are included in kernel versions 2.4.20 or greater.
CREDITS
Arduino is an open source project, supported by many.
The Arduino team is composed of Massimo Banzi, David Cuartielles, Tom Igoe,
Gianluca Martino, and David A. Mellis.
Arduino uses the GNU avr-gcc toolchain, avrdude, avr-libc, and code from
Processing and Wiring.
Icon Design and Artwork created by Thomas Glaser (envis precisely).

View File

@ -2,13 +2,11 @@
Appending to Strings using the += operator and concat()
Examples of how to append different data types to strings
The circuit: No external hardware needed.
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
String stringOne, stringTwo;

View File

@ -0,0 +1,45 @@
/*
String Case changes
Examples of how to change the case of a string
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println("\n\nString case changes:");
}
void loop() {
// toUpperCase() changes all letters to upper case:
String stringOne = "<html><head><body>";
Serial.println(stringOne.toUpperCase());
// toLowerCase() changes all letters to lower case:
String stringTwo = "</BODY></HTML>";
Serial.println(stringTwo.toLowerCase());
// do nothing while true:
while(true);
}

View File

@ -0,0 +1,56 @@
/*
String charAt() and setCharAt()
Examples of how to get and set characters of a String
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println("\n\nString charAt() and setCharAt():");
}
void loop() {
// make a string to report a sensor reading:
String reportString = "SensorReading: 456";
Serial.println(reportString);
// the reading's most significant digit is at position 15 in the reportString:
String mostSignificantDigit = reportString.charAt(15);
Serial.println("Most significant digit of the sensor reading is: " + mostSignificantDigit);
// add blank space:
Serial.println();
// you can alo set the character of a string. Change the : to a = character
reportString.setCharAt(13, '=');
Serial.println(reportString);
// do nothing while true:
while(true);
}

View File

@ -3,14 +3,12 @@
Examples of how to compare strings using the comparison operators
The circuit: No external hardware needed.
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
String stringOne, stringTwo;
void setup() {

View File

@ -3,13 +3,10 @@
Examples of how to create strings from other data types
The circuit: No external hardware needed.
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {

View File

@ -0,0 +1,72 @@
/*
String indexOf() and lastIndexOf() functions
Examples of how to evaluate, look for, and replace characters in a String
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println("\n\nString indexOf() and lastIndexOf() functions:");
}
void loop() {
// indexOf() returns the position (i.e. index) of a particular character
// in a string. For example, if you were parsing HTML tags, you could use it:
String stringOne = "<HTML><HEAD><BODY>";
int firstClosingBracket = stringOne.indexOf('>');
Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);
stringOne = "<HTML><HEAD><BODY>";
int secondOpeningBracket = firstClosingBracket + 1;
int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket );
Serial.println("The index of the second > in the string " + stringOne + " is " + secondClosingBracket);
// you can also use indexOf() to search for Strings:
stringOne = "<HTML><HEAD><BODY>";
int bodyTag = stringOne.indexOf("<BODY>");
Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);
stringOne = "<UL><LI>item<LI>item<LI>item</UL>";
int firstListItem = stringOne.indexOf("<LI>");
int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);
// lastIndexOf() gives you the last occurrence of a character or string:
int lastOpeningBracket = stringOne.lastIndexOf('<');
Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket);
int lastListItem = stringOne.lastIndexOf("<LI>");
Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);
// lastIndexOf() can also search for a string:
stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
int lastParagraph = stringOne.lastIndexOf("<p");
int penultimateParagraph = stringOne.lastIndexOf("<p", lastParagraph - 1);
Serial.println("The index of the second last paragraph tag " + stringOne + " is " + penultimateParagraph);
// do nothing while true:
while(true);
}

View File

@ -0,0 +1,52 @@
/*
String length() and trim()
Examples of how to use length() and trim() in a String
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println("\n\nString length() and trim():");
}
void loop() {
// here's a String with empty spaces at the end (called white space):
String stringOne = "Hello! ";
Serial.print(stringOne);
Serial.print("<--- end of string. Length: ");
Serial.println(stringOne.length());
// trim the white space off the string:
stringOne = stringOne.trim();
Serial.print(stringOne);
Serial.print("<--- end of trimmed string. Length: ");
Serial.println(stringOne.length());
// do nothing while true:
while(true);
}

View File

@ -0,0 +1,52 @@
/*
String replace()
Examples of how to replace characters or substrings of a string
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println("\n\nString replace:");
}
void loop() {
String stringOne = "<html><head><body>";
Serial.println(stringOne);
// replace() changes all instances of one substring with another:
String stringTwo = stringOne.replace("<", "</");
Serial.println(stringTwo);
// you can also use replace() on single characters:
String normalString = "bookkeeper";
Serial.println("normal: " + normalString);
String leetString = normalString.replace('o', '0');
leetString = leetString.replace('e', '3');
Serial.println("l33tspeak: " + leetString);
// do nothing while true:
while(true);
}

View File

@ -0,0 +1,62 @@
/*
String startWith() and endsWith()
Examples of how to use startsWith() and endsWith() in a String
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println("\n\nString startsWith() and endsWith():");
}
void loop() {
// startsWith() checks to see if a String starts with a particular substring:
String stringOne = "HTTP/1.1 200 OK";
Serial.println(stringOne);
if (stringOne.startsWith("HTTP/1.1")) {
Serial.println("Server's using http version 1.1");
}
// you can also look for startsWith() at an offset position in the string:
stringOne = "HTTP/1.1 200 OK";
if (stringOne.startsWith("200 OK", 9)) {
Serial.println("Got an OK from the server");
}
// endsWith() checks to see if a String ends with a particular character:
String sensorReading = "sensor = ";
sensorReading += analogRead(0);
Serial.print (sensorReading);
if (sensorReading.endsWith(0)) {
Serial.println(". This reading is divisible by ten");
}
else {
Serial.println(". This reading is not divisible by ten");
}
// do nothing while true:
while(true);
}

View File

@ -0,0 +1,50 @@
/*
String substring()
Examples of how to use substring in a String
created 27 July 2010
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println("\n\nString substring():");
}
void loop() {
// startsWith() checks to see if a String starts with a particular substring:
String stringOne = "Content-Type: text/html";
Serial.println(stringOne);
// substring(index) looks for the substring from the index position to the end:
if (stringOne.substring(19) == "html") {
Serial.println("It's an html file");
}
// you can also look for a substring in the middle of a string:
if (stringOne.substring(14,18) == "text") {
Serial.println("It's a text-based file");
}
// do nothing while true:
while(true);
}

View File

@ -37,7 +37,7 @@ class String
String( const unsigned int, const int base=10 );
String( const long, const int base=10 );
String( const unsigned long, const int base=10 );
~String() { free(_buffer); }
~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0;
// operators
const String & operator = ( const String &rhs );