Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand String examples with explicit size concat and constructor #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/08.Strings/StringAppendOperator/StringAppendOperator.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ void loop() {
stringTwo.concat(millis());
Serial.println(stringTwo); // prints "The millis(): 43534" or whatever the value of the millis() is

#if ARDUINO_API_VERSION >= 10000
// using concat with an explicit length argument to add only a part of
// a string:
stringOne = "Only part: ";
char *to_add = "use this but not this";
stringOne.concat(to_add, 8);
Serial.println(stringOne); // prints "Only part: use this"

// using concat with an explicit length argument to add a
// non-zero-terminated string / char array (note that it will be
// terminated inside the String object).
stringTwo = "Unterminated: ";
char unterminated[] = {'n', 'o', 'n', 'u', 'l'};
stringTwo.concat(unterminated, sizeof(unterminated));
Serial.println(stringTwo); // prints "Unterminated: nonul"
#endif // ARDUINO_API_VERSION

// do nothing while true:
while (true);
}
15 changes: 15 additions & 0 deletions examples/08.Strings/StringConstructors/StringConstructors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ void loop() {
stringOne = String(5.698, 2);
Serial.println(stringOne);

#if ARDUINO_API_VERSION >= 10000
// Using an explicit length argument to to use only a part of a
// string:
char *to_add = "use this but not this";
stringOne = String(to_add, 8);
Serial.println(stringOne); // prints "use this"

// using explicit length argument to add a non-zero-terminated string
// / char array (note that it will be terminated inside the String
// object).
char unterminated[] = {'n', 'o', 'n', 'u', 'l'};
stringTwo = String(unterminated, sizeof(unterminated));
Serial.println(stringTwo); // prints "nonul"
#endif // ARDUINO_API_VERSION

// do nothing while true:
while (true);

Expand Down