Skip to content

Commit 89fb1d7

Browse files
committed
Corretions and refinements to the String examples
1 parent 495f1f8 commit 89fb1d7

File tree

17 files changed

+246
-223
lines changed

17 files changed

+246
-223
lines changed
Binary file not shown.

examples/08.Strings/CharacterAnalysis/CharacterAnalysis.ino

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void setup() {
1616
Serial.begin(9600);
1717
// this check is only needed on the Leonardo:
1818
while (!Serial) ;
19-
;
19+
;
2020

2121
// send an intro:
2222
Serial.println("send any byte and I'll tell you everything I can about it");
@@ -82,8 +82,3 @@ void loop() {
8282
}
8383
}
8484

85-
86-
87-
88-
89-

examples/08.Strings/StringAdditionOperator/StringAdditionOperator.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ void setup() {
2121
Serial.begin(9600);
2222
// this check is only needed on the Leonardo:
2323
while (!Serial) ;
24-
;
25-
24+
;
25+
2626
stringOne = String("stringThree = ");
2727
stringTwo = String("this string");
2828
stringThree = String ();
@@ -63,4 +63,4 @@ void loop() {
6363

6464
// do nothing while true:
6565
while(true);
66-
}
66+
}

examples/08.Strings/StringAppendOperator/StringAppendOperator.ino

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
String stringOne, stringTwo;
1515

1616
void setup() {
17-
// Open serial communications and wait for port to open:
17+
// Open serial communications and wait for port to open:
1818
Serial.begin(9600);
1919
// this check is only needed on the Leonardo:
2020
while (!Serial) ;
21-
;
21+
;
2222

2323
stringOne = String("Sensor ");
2424
stringTwo = String("value");
@@ -66,4 +66,5 @@ void loop() {
6666

6767
// do nothing while true:
6868
while(true);
69-
}
69+
}
70+

examples/08.Strings/StringCaseChanges/StringCaseChanges.ino

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
*/
1414

1515
void setup() {
16-
// Open serial communications and wait for port to open:
16+
// Open serial communications and wait for port to open:
1717
Serial.begin(9600);
1818
// this check is only needed on the Leonardo:
1919
while (!Serial) ;
20-
;
20+
;
2121

2222
Serial.println("\n\nString case changes:");
2323
}
@@ -26,16 +26,16 @@ void loop() {
2626
// toUpperCase() changes all letters to upper case:
2727
String stringOne = "<html><head><body>";
2828
Serial.println(stringOne);
29-
stringOne = (stringOne.toUpperCase());
29+
stringOne.toUpperCase();
3030
Serial.println(stringOne);
31-
32-
// toLowerCase() changes all letters to lower case:
31+
32+
// toLowerCase() changes all letters to lower case:
3333
String stringTwo = "</BODY></HTML>";
3434
Serial.println(stringTwo);
35-
stringTwo = stringTwo.toLowerCase();
35+
stringTwo.toLowerCase();
3636
Serial.println(stringTwo);
37-
38-
37+
38+
3939
// do nothing while true:
4040
while(true);
4141
}

examples/08.Strings/StringCharacters/StringCharacters.ino

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void setup() {
1717
Serial.begin(9600);
1818
// this check is only needed on the Leonardo:
1919
while (!Serial) ;
20-
;
20+
;
2121

2222
Serial.println("\n\nString charAt() and setCharAt():");
2323
}
@@ -26,18 +26,19 @@ void loop() {
2626
// make a string to report a sensor reading:
2727
String reportString = "SensorReading: 456";
2828
Serial.println(reportString);
29-
29+
3030
// the reading's most significant digit is at position 15 in the reportString:
31-
String mostSignificantDigit = reportString.charAt(15);
31+
char mostSignificantDigit = reportString.charAt(15);
3232
Serial.println("Most significant digit of the sensor reading is: " + mostSignificantDigit);
3333

34-
// add blank space:
34+
// add blank space:
3535
Serial.println();
36-
36+
3737
// you can alo set the character of a string. Change the : to a = character
3838
reportString.setCharAt(13, '=');
3939
Serial.println(reportString);
4040

4141
// do nothing while true:
4242
while(true);
43-
}
43+
}
44+

examples/08.Strings/StringComparisonOperators/StringComparisonOperators.ino

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
1212
This example code is in the public domain.
1313
*/
14-
14+
1515
String stringOne, stringTwo;
1616

1717
void setup() {
18-
// Open serial communications and wait for port to open:
18+
// Open serial communications and wait for port to open:
1919
Serial.begin(9600);
2020
// this check is only needed on the Leonardo:
2121
while (!Serial) ;
22-
;
22+
;
2323

2424
stringOne = String("this");
2525
stringTwo = String("that");
@@ -62,7 +62,7 @@ void loop() {
6262
// a numeric string compared to the number it represents:
6363
stringOne = "1";
6464
int numberOne = 1;
65-
if (stringOne == numberOne) {
65+
if (stringOne.toInt() == numberOne) {
6666
Serial.println(stringOne + " = " + numberOne);
6767
}
6868

@@ -126,4 +126,4 @@ void loop() {
126126

127127
}
128128
}
129-
}
129+
}

examples/08.Strings/StringConstructors/StringConstructors.ino

+41-35
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,58 @@
1313
*/
1414

1515
void setup() {
16+
// Open serial communications and wait for port to open:
1617
Serial.begin(9600);
18+
// this check is only needed on the Leonardo:
19+
while (!Serial) ;
20+
;
21+
22+
Serial.println("\n\nString Constructors:");
1723
}
1824

1925
void loop() {
20-
// using a constant String:
21-
String stringOne = "Hello String";
22-
Serial.println(stringOne); // prints "Hello String"
26+
// using a constant String:
27+
String stringOne = "Hello String";
28+
Serial.println(stringOne); // prints "Hello String"
29+
30+
// converting a constant char into a String:
31+
stringOne = String('a');
32+
Serial.println(stringOne); // prints "a"
2333

24-
// converting a constant char into a String:
25-
stringOne = String('a');
26-
Serial.println(stringOne); // prints "a"
34+
// converting a constant string into a String object:
35+
String stringTwo = String("This is a string");
36+
Serial.println(stringTwo); // prints "This is a string"
2737

28-
// converting a constant string into a String object:
29-
String stringTwo = String("This is a string");
30-
Serial.println(stringTwo); // prints "This is a string"
38+
// concatenating two strings:
39+
stringOne = String(stringTwo + " with more");
40+
// prints "This is a string with more":
41+
Serial.println(stringOne);
3142

32-
// concatenating two strings:
33-
stringOne = String(stringTwo + " with more");
34-
// prints "This is a string with more":
35-
Serial.println(stringOne);
43+
// using a constant integer:
44+
stringOne = String(13);
45+
Serial.println(stringOne); // prints "13"
3646

37-
// using a constant integer:
38-
stringOne = String(13);
39-
Serial.println(stringOne); // prints "13"
40-
41-
// using an int and a base:
42-
stringOne = String(analogRead(A0), DEC);
43-
// prints "453" or whatever the value of analogRead(A0) is
44-
Serial.println(stringOne);
47+
// using an int and a base:
48+
stringOne = String(analogRead(A0), DEC);
49+
// prints "453" or whatever the value of analogRead(A0) is
50+
Serial.println(stringOne);
4551

46-
// using an int and a base (hexadecimal):
47-
stringOne = String(45, HEX);
48-
// prints "2d", which is the hexadecimal version of decimal 45:
49-
Serial.println(stringOne);
52+
// using an int and a base (hexadecimal):
53+
stringOne = String(45, HEX);
54+
// prints "2d", which is the hexadecimal version of decimal 45:
55+
Serial.println(stringOne);
5056

51-
// using an int and a base (binary)
52-
stringOne = String(255, BIN);
53-
// prints "11111111" which is the binary value of 255
54-
Serial.println(stringOne);
55-
56-
// using a long and a base:
57-
stringOne = String(millis(), DEC);
58-
// prints "123456" or whatever the value of millis() is:
59-
Serial.println(stringOne);
57+
// using an int and a base (binary)
58+
stringOne = String(255, BIN);
59+
// prints "11111111" which is the binary value of 255
60+
Serial.println(stringOne);
61+
62+
// using a long and a base:
63+
stringOne = String(millis(), DEC);
64+
// prints "123456" or whatever the value of millis() is:
65+
Serial.println(stringOne);
6066

6167
// do nothing while true:
6268
while(true);
6369

64-
}
70+
}

examples/08.Strings/StringIndexOf/StringIndexOf.ino

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
*/
1414

1515
void setup() {
16-
// Open serial communications and wait for port to open:
16+
// Open serial communications and wait for port to open:
1717
Serial.begin(9600);
1818
// this check is only needed on the Leonardo:
1919
while (!Serial) ;
20-
;
20+
;
2121

2222
Serial.println("\n\nString indexOf() and lastIndexOf() functions:");
2323

2424
}
2525

2626
void loop() {
27-
// indexOf() returns the position (i.e. index) of a particular character
27+
// indexOf() returns the position (i.e. index) of a particular character
2828
// in a string. For example, if you were parsing HTML tags, you could use it:
2929
String stringOne = "<HTML><HEAD><BODY>";
3030
int firstClosingBracket = stringOne.indexOf('>');
@@ -53,12 +53,13 @@ void loop() {
5353
Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);
5454

5555

56-
// lastIndexOf() can also search for a string:
57-
stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
56+
// lastIndexOf() can also search for a string:
57+
stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
5858
int lastParagraph = stringOne.lastIndexOf("<p");
5959
int secondLastGraf = stringOne.lastIndexOf("<p", lastParagraph - 1);
6060
Serial.println("The index of the second last paragraph tag " + stringOne + " is " + secondLastGraf);
6161

62-
// do nothing while true:
63-
while(true);
64-
}
62+
// do nothing while true:
63+
while(true);
64+
}
65+

examples/08.Strings/StringLength/StringLength.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ String txtMsg = ""; // a string for incoming text
1515
int lastStringLength = txtMsg.length(); // previous length of the String
1616

1717
void setup() {
18-
// open the serial port:
18+
// Open serial communications and wait for port to open:
1919
Serial.begin(9600);
20+
// this check is only needed on the Leonardo:
21+
while (!Serial) ;
22+
;
23+
24+
Serial.println("\n\nString length():");
2025
}
2126

2227
void loop() {
@@ -40,4 +45,4 @@ void loop() {
4045
// note the length for next time through the loop:
4146
lastStringLength = txtMsg.length();
4247
}
43-
}
48+
}

examples/08.Strings/StringLengthTrim/StringLengthTrim.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
*/
1414

1515
void setup() {
16-
// Open serial communications and wait for port to open:
16+
// Open serial communications and wait for port to open:
1717
Serial.begin(9600);
1818
// this check is only needed on the Leonardo:
1919
while (!Serial) ;
20-
;
20+
;
2121

2222
Serial.println("\n\nString length() and trim():");
2323
}
@@ -30,7 +30,7 @@ void loop() {
3030
Serial.println(stringOne.length());
3131

3232
// trim the white space off the string:
33-
stringOne = stringOne.trim();
33+
stringOne.trim();
3434
Serial.print(stringOne);
3535
Serial.print("<--- end of trimmed string. Length: ");
3636
Serial.println(stringOne.length());
12 KB
Binary file not shown.

examples/08.Strings/StringReplace/StringReplace.ino

+15-8
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,36 @@
1313
*/
1414

1515
void setup() {
16-
// Open serial communications and wait for port to open:
16+
// Open serial communications and wait for port to open:
1717
Serial.begin(9600);
1818
// this check is only needed on the Leonardo:
1919
while (!Serial) ;
20-
;
20+
;
2121

22-
Serial.println("\n\nString replace:");
22+
Serial.println("\n\nString replace:\n");
2323
}
2424

2525
void loop() {
2626
String stringOne = "<html><head><body>";
2727
Serial.println(stringOne);
2828
// replace() changes all instances of one substring with another:
29-
String stringTwo = stringOne.replace("<", "</");
30-
Serial.println(stringTwo);
29+
// first, make a copy of th original string:
30+
String stringTwo = stringOne;
31+
// then perform the replacements:
32+
stringTwo.replace("<", "</");
33+
// print the original:
34+
Serial.println("Original string: " + stringOne);
35+
// and print the modified string:
36+
Serial.println("Modified string: " + stringTwo);
3137

3238
// you can also use replace() on single characters:
3339
String normalString = "bookkeeper";
3440
Serial.println("normal: " + normalString);
35-
String leetString = normalString.replace('o', '0');
36-
leetString = leetString.replace('e', '3');
41+
String leetString = normalString;
42+
leetString.replace('o', '0');
43+
leetString.replace('e', '3');
3744
Serial.println("l33tspeak: " + leetString);
3845

3946
// do nothing while true:
4047
while(true);
41-
}
48+
}

0 commit comments

Comments
 (0)