From bc2c88789fe9e7e71e63fc25a8e9de958565b953 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 27 Jul 2010 19:03:52 +0000 Subject: [PATCH] Added core String library examples --- .../StringAdditionOperator.pde | 73 ++++++++++++++++ .../StringAdditionOperator/data/readme.txt | 41 +++++++++ .../StringAppendOperator.pde | 70 ++++++++++++++++ .../StringComparisonOperators.pde | 82 ++++++++++++++++++ .../StringConstructors/StringConstructors.pde | 60 ++++++++++++++ .../StringGeneralOperators.pde | 83 +++++++++++++++++++ 6 files changed, 409 insertions(+) create mode 100644 build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde create mode 100644 build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt create mode 100644 build/shared/examples/8.Strings/StringAppendOperator/StringAppendOperator.pde create mode 100644 build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde create mode 100644 build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde create mode 100644 build/shared/examples/8.Strings/StringGeneralOperators/StringGeneralOperators.pde diff --git a/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde b/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde new file mode 100644 index 000000000..a4eaa2e7f --- /dev/null +++ b/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde @@ -0,0 +1,73 @@ +/* + Adding Strings together + + 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: +String stringOne, stringTwo, stringThree; + +void setup() { + Serial.begin(9600); + stringOne = String("stringThree = "); + stringTwo = String("this string"); + stringThree = String (); + Serial.println("\n\nAdding strings together (concatenation):"); +} + +void loop() { + // adding a constant integer to a string: + stringThree = stringOne + 123; + Serial.println(stringThree); // prints "You added 123" + + // adding a constant long interger to a string: + stringThree = stringOne + 123456789; + Serial.println(stringThree); // prints " You added 123456789" + + // adding a constant character to a string: + stringThree = stringOne + 'A'; + Serial.println(stringThree); // prints "You added A" + + // adding a constant string to a string: + stringThree = stringOne + "abc"; + Serial.println(stringThree); // prints "You added abc" + + stringThree = stringOne + stringTwo; + Serial.println(stringThree); // prints "You added this string" + + // adding a variable integer to a string: + int sensorValue = analogRead(0); + stringOne = "Sensor value: "; + stringThree = stringOne + sensorValue; + Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(0) has + + // adding a variable long integer to a string: + long currentTime = millis(); + stringOne="millis() value: "; + stringThree = stringOne + millis(); + Serial.println(stringThree); // prints "The millis: 345345" or whatever value currentTime has + + // do nothing while true: + while(true); +} + + + + + + + + + + + + diff --git a/build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt b/build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt new file mode 100644 index 000000000..d82c3f753 --- /dev/null +++ b/build/shared/examples/8.Strings/StringAdditionOperator/data/readme.txt @@ -0,0 +1,41 @@ +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 new file mode 100644 index 000000000..ba01c2e7e --- /dev/null +++ b/build/shared/examples/8.Strings/StringAppendOperator/StringAppendOperator.pde @@ -0,0 +1,70 @@ +/* + 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; + +void setup() { + Serial.begin(9600); + stringOne = String("Sensor "); + stringTwo = String("value"); + Serial.println("\n\nAppending to a string:"); +} + +void loop() { + Serial.println(stringOne); // prints "Sensor " + + // adding a string to a string: + stringOne += stringTwo; + Serial.println(stringOne); // prints "Sensor value" + + // adding a constant string to a string: + stringOne += " for input "; + Serial.println(stringOne); // prints "Sensor value for input" + + // adding a constant character to a string: + stringOne += 'A'; + Serial.println(stringOne); // prints "Sensor value for input A" + + // adding a constant integer to a string: + stringOne += 0; + Serial.println(stringOne); // prints "Sensor value for input A0" + + // adding a constant string to a string: + stringOne += ": "; + Serial.println(stringOne); // prints "Sensor value for input" + + // adding a variable integer to a string: + stringOne += analogRead(0); + Serial.println(stringOne); // prints "Sensor value for input A0: 456" or whatever analogRead(0) is + + Serial.println("\n\nchanging the Strings' values"); + stringOne = "A long integer: "; + stringTwo = "The millis(): "; + + // adding a constant long integer to a string: + stringOne += 123456789; + Serial.println(stringOne); // prints "A long integer: 123456789" + + // using concat() to add a long variable to a string: + stringTwo.concat(millis()); + Serial.println(stringTwo); // prints "The millis(): 43534" or whatever the value of the millis() is + + // 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 new file mode 100644 index 000000000..5cb891250 --- /dev/null +++ b/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde @@ -0,0 +1,82 @@ +/* + Comparing Strings + + 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() { + Serial.begin(9600); + stringOne = String("this"); + stringTwo = String("that"); + Serial.println("\n\nAdding strings together (concatenation):"); + +} + +void loop() { + // two strings equal: + if (stringOne == "this") { + Serial.println("StringOne == \"this\""); + } + // two strings not equal: + if (stringOne != stringTwo) { + Serial.println(stringOne + " =! " + stringTwo); + } + + // two strings not equal (case sensitivity matters): + stringOne = "This"; + stringTwo = "this"; + if (stringOne != stringTwo) { + Serial.println(stringOne + " =! " + stringTwo); + } + + // a numeric string compared to the number it represents: + stringOne = "1"; + int numberOne = 1; + if (stringOne == numberOne) { + Serial.println(stringOne + " = " + numberOne); + } + + // two numeric strings compared: + stringOne = "2"; + stringTwo = "1"; + if (stringOne >= stringTwo) { + Serial.println(stringOne + " >= " + stringTwo); + } + +// comparison operators can be used to compare strings for alphabetic sorting too: + stringOne = String("Brown"); + if (stringOne < "Charles") { + Serial.println(stringOne + " < Charles"); + } + + if (stringOne > "Adams") { + Serial.println(stringOne + " > Adams"); + } + + if (stringOne <= "Browne") { + Serial.println(stringOne + " <= Browne"); + } + + + if (stringOne >= "Brow") { + Serial.println(stringOne + " >= Brow"); + } + + // do nothing while true: + while(true); +} + + + + + + diff --git a/build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde b/build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde new file mode 100644 index 000000000..a63e461af --- /dev/null +++ b/build/shared/examples/8.Strings/StringConstructors/StringConstructors.pde @@ -0,0 +1,60 @@ +/* + String constructors + + 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() { + Serial.begin(9600); +} + +void loop() { + + String stringOne = "Hello String"; // using a constant String + Serial.println(stringOne); // prints "Hello String" + + stringOne = String('a'); // converting a constant char into a String + Serial.println(stringOne); // prints "a" + + String stringTwo = String("This is a string"); // converting a constant string into a String object + Serial.println(stringTwo); // prints "This is a string" + + stringOne = String(stringTwo + " with more"); // concatenating two strings + Serial.println(stringOne); // prints "This is a string with more" + + stringOne = String(13); // using a constant integer + Serial.println(stringOne); // prints "13" + + stringOne = String(analogRead(0), DEC); // using an int and a base + Serial.println(stringOne); // prints "453" or whatever the value of analogRead(0) is + + stringOne = String(45, HEX); // using an int and a base (hexadecimal) + Serial.println(stringOne); // prints "2d", which is the hexadecimal version of decimal 45 + + stringOne = String(255, BIN); // using an int and a base (binary) + Serial.println(stringOne); // prints "11111111" which is the binary value of 255 + + + stringOne = String(millis(), DEC); // using a long and a base + Serial.println(stringOne); // prints "123456" or whatever the value of millis() is + + // do nothing while true: + while(true); + +} + + + + + + + + diff --git a/build/shared/examples/8.Strings/StringGeneralOperators/StringGeneralOperators.pde b/build/shared/examples/8.Strings/StringGeneralOperators/StringGeneralOperators.pde new file mode 100644 index 000000000..9708d6bfd --- /dev/null +++ b/build/shared/examples/8.Strings/StringGeneralOperators/StringGeneralOperators.pde @@ -0,0 +1,83 @@ +String stringOne = String("string number one"); // declares stringOne with an initial string +char[] theseChars; +void setup() { + Serial.begin(9600); + + String stringTwo = String(10000); // stringTwo holds "10000", equivalent to: String stringTwo = 10000; + Serial.println("stringTwo holds: " + stringTwo); + + String stringThree = String(567000, DEC); // stringThree holds "567000" + Serial.println("stringThree holds: " + stringThree); + + String stringFour = "string number four"; // stringFour holds "string number four" + Serial.println("stringFour holds: " + stringFour); + + stringFour += " plus another part"; // use + operator to add a string + Serial.println("now stringFour holds: " + stringFour); // user + operator when printing to serial + + char c = stringFour.charAt(5); // get character at position 5 + Serial.print("c holds: "); + Serial.println(c); // prints 'g' + + String stringFive = String("string number one"); // stringFive holds "string number one" + + int cmp = stringOne.compareTo(stringFive); // compare stringOne to stringFive + if(cmp == 0) { + Serial.println("stringOne and stringFive are equal"); // prints equal + } else { + Serial.println("stringOne and stringFive are different"); + } + + if(stringFive.endsWith("one")) // check if stringFive ends with "one" + Serial.println("stringFive ends with \"one\""); // prints "stringFive ends with "one"" + + if(stringOne.equals(stringFive)) // check if stringOne equal to stringFive + Serial.println("stringOne and stringFive are equal"); // prints equal + + String stringSix = String("string NUMBER one"); // stringSix holds "string NUMBER one" + if(stringFive.equalsIgnoreCase(stringSix)) // check if stringFive and stringSix are equal ignoring case differences + Serial.println("stringSix and stringFive are equal ignoring the case"); // prints equal + + Serial.print("index of char R pn stringSix is: "); + Serial.println(stringSix.indexOf('R'), DEC); // prints 12 + + Serial.print("index of char R on stringSix from index 13 is: "); + Serial.println(stringSix.indexOf('R', 13), DEC); // prints -1, not found + + String s7 = stringSix.substring(7, 13); + Serial.println("s7 is: "+s7); // prints "NUMBER" + + Serial.print("index of string \"NUMBER\" on stringSix is: "); + Serial.println(stringSix.indexOf(s7), DEC); // prints 7 + + Serial.print("last index of char 'n' on stringSix is: "); + Serial.println(stringSix.lastIndexOf('n'), DEC); // prints 15 + + Serial.print("length of stringSix is: "); + Serial.println(stringSix.length(), DEC); // prints 15 + + stringSix.setCharAt(15, 'N'); // set character at index 15 to 'N' + Serial.println("stringSix is: "+stringSix); // prints "string NUMBER oNe" + + if(stringSix.startsWith("string")) // check if stringSix starts with "string" + Serial.println("stringSix starts with \"string\""); // stringSix starts with "string" string + + Serial.println("stringSix to lower case is: "+stringSix.toLowerCase()); // prints "string number one" + + Serial.println("stringSix to upper case is: "+stringSix.toUpperCase()); // prints "STRING NUMBER ONE" + + stringSix.concat(" plus spaces at the end "); // concat a string + Serial.println("stringSix is: "+stringSix); // prints "string NUMBER oNe plus spaces at the end " + + Serial.println("trim of stringSix is: "+stringSix.trim()); // prints "string NUMBER oNe plus spaces at the end" + + Serial.println("stringSix is: "+stringSix.replace('e', '5')); // prints "string NUMBER oN5 plus spac5s at th5 5nd" + + const char *str = stringSix.toCharArray(); // get the char array + Serial.print("str is: "); + Serial.println(str); // prints "string NUMBER oNe plus spaces at the end " +} + +void loop() { + +}