From 81e7814887a70e046e5e474ff877b7239fc043a5 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 27 Jul 2010 22:59:18 +0000 Subject: [PATCH] added more String examples --- .../StringAdditionOperator.pde | 5 +- .../StringAdditionOperator/data/readme.txt | 41 ----------- .../StringAppendOperator.pde | 2 - .../StringCaseChanges/StringCaseChanges.pde | 45 ++++++++++++ .../StringCharacters/StringCharacters.pde | 56 +++++++++++++++ .../StringComparisonOperators.pde | 4 +- .../StringConstructors/StringConstructors.pde | 3 - .../8.Strings/StringIndexOf/StringIndexOf.pde | 72 +++++++++++++++++++ .../StringLengthTrim/StringLengthTrim.pde | 52 ++++++++++++++ .../8.Strings/StringReplace/StringReplace.pde | 52 ++++++++++++++ .../StringStartsWithEndsWith.pde | 62 ++++++++++++++++ .../StringSubstring/StringSubstring.pde | 50 +++++++++++++ hardware/arduino/cores/arduino/WString.h | 2 +- 13 files changed, 392 insertions(+), 54 deletions(-) delete mode 100644 build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt create mode 100644 build/shared/examples/8.Strings/StringCaseChanges/StringCaseChanges.pde create mode 100644 build/shared/examples/8.Strings/StringCharacters/StringCharacters.pde create mode 100644 build/shared/examples/8.Strings/StringIndexOf/StringIndexOf.pde create mode 100644 build/shared/examples/8.Strings/StringLengthTrim/StringLengthTrim.pde create mode 100644 build/shared/examples/8.Strings/StringReplace/StringReplace.pde create mode 100644 build/shared/examples/8.Strings/StringStartsWithEndsWith/StringStartsWithEndsWith.pde create mode 100644 build/shared/examples/8.Strings/StringSubstring/StringSubstring.pde diff --git a/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde b/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde index a4eaa2e7f..10a641018 100644 --- a/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde +++ b/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde @@ -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. - + This example code is in the public domain. */ // declare three strings: diff --git a/build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt b/build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt deleted file mode 100644 index d82c3f753..000000000 --- a/build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt +++ /dev/null @@ -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). - diff --git a/build/shared/examples/8.Strings/StringAppendOperator/StringAppendOperator.pde b/build/shared/examples/8.Strings/StringAppendOperator/StringAppendOperator.pde index ba01c2e7e..2f22b1f51 100644 --- a/build/shared/examples/8.Strings/StringAppendOperator/StringAppendOperator.pde +++ b/build/shared/examples/8.Strings/StringAppendOperator/StringAppendOperator.pde @@ -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; diff --git a/build/shared/examples/8.Strings/StringCaseChanges/StringCaseChanges.pde b/build/shared/examples/8.Strings/StringCaseChanges/StringCaseChanges.pde new file mode 100644 index 000000000..42316e300 --- /dev/null +++ b/build/shared/examples/8.Strings/StringCaseChanges/StringCaseChanges.pde @@ -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 = ""; + Serial.println(stringOne.toUpperCase()); + + // toLowerCase() changes all letters to lower case: + String stringTwo = ""; + Serial.println(stringTwo.toLowerCase()); + + // do nothing while true: + while(true); +} + + + + + + + + + + + + + + + + + diff --git a/build/shared/examples/8.Strings/StringCharacters/StringCharacters.pde b/build/shared/examples/8.Strings/StringCharacters/StringCharacters.pde new file mode 100644 index 000000000..ff29aea09 --- /dev/null +++ b/build/shared/examples/8.Strings/StringCharacters/StringCharacters.pde @@ -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); +} + + + + + + + + + + + + + + + + + + + + + diff --git a/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde b/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde index 425262f4b..0e6bb748b 100644 --- a/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde +++ b/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde @@ -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() { diff --git a/build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde b/build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde index a63e461af..cff1419f7 100644 --- a/build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde +++ b/build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde @@ -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() { diff --git a/build/shared/examples/8.Strings/StringIndexOf/StringIndexOf.pde b/build/shared/examples/8.Strings/StringIndexOf/StringIndexOf.pde new file mode 100644 index 000000000..218d6552f --- /dev/null +++ b/build/shared/examples/8.Strings/StringIndexOf/StringIndexOf.pde @@ -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 = ""; + int firstClosingBracket = stringOne.indexOf('>'); + Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket); + + stringOne = ""; + 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 = ""; + int bodyTag = stringOne.indexOf(""); + Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag); + + stringOne = ""; + int firstListItem = stringOne.indexOf("
  • "); + 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("
  • "); + Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem); + + +// lastIndexOf() can also search for a string: + stringOne = "

    Lorem ipsum dolor sit amet

    Ipsem

    Quod

    "; + int lastParagraph = stringOne.lastIndexOf("