Skip to content

Commit 960fcd3

Browse files
Expand String examples with explicit size concat and constructor
This expands examples to show the newly added APIs: String::concat(const char *, unsigned int) String::String(const char *, unsigned int) Since not all cores versions will support this right away, a version check is added against ARDUINO_CORE_API.
1 parent c33a3e9 commit 960fcd3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

build/shared/examples/08.Strings/StringAppendOperator/StringAppendOperator.ino

+17
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ void loop() {
6767
stringTwo.concat(millis());
6868
Serial.println(stringTwo); // prints "The millis(): 43534" or whatever the value of the millis() is
6969

70+
#if ARDUINO_API_VERSION >= 10000
71+
// using concat with an explicit length argument to add only a part of
72+
// a string:
73+
stringOne = "Only part: ";
74+
char *to_add = "use this but not this";
75+
stringOne.concat(to_add, 8);
76+
Serial.println(stringOne); // prints "Only part: use this"
77+
78+
// using concat with an explicit length argument to add a
79+
// non-zero-terminated string / char array (note that it will be
80+
// terminated inside the String object).
81+
stringTwo = "Unterminated: ";
82+
char unterminated[] = {'n', 'o', 'n', 'u', 'l'};
83+
stringTwo.concat(unterminated, sizeof(unterminated));
84+
Serial.println(stringTwo); // prints "Unterminated: nonul"
85+
#endif // ARDUINO_API_VERSION
86+
7087
// do nothing while true:
7188
while (true);
7289
}

build/shared/examples/08.Strings/StringConstructors/StringConstructors.ino

+15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ void loop() {
7474
stringOne = String(5.698, 2);
7575
Serial.println(stringOne);
7676

77+
#if ARDUINO_API_VERSION >= 10000
78+
// Using an explicit length argument to to use only a part of a
79+
// string:
80+
char *to_add = "use this but not this";
81+
stringOne = String(to_add, 8);
82+
Serial.println(stringOne); // prints "use this"
83+
84+
// using explicit length argument to add a non-zero-terminated string
85+
// / char array (note that it will be terminated inside the String
86+
// object).
87+
char unterminated[] = {'n', 'o', 'n', 'u', 'l'};
88+
stringTwo = String(unterminated, sizeof(unterminated));
89+
Serial.println(stringTwo); // prints "nonul"
90+
#endif // ARDUINO_API_VERSION
91+
7792
// do nothing while true:
7893
while (true);
7994

0 commit comments

Comments
 (0)