| 1 |
/* |
| 2 |
String length() and trim() |
| 3 |
|
| 4 |
Examples of how to use length() and trim() in a String |
| 5 |
|
| 6 |
created 27 Jul 2010 |
| 7 |
modified 2 Apr 2012 |
| 8 |
by Tom Igoe |
| 9 |
|
| 10 |
This example code is in the public domain. |
| 11 |
|
| 12 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringLengthTrim |
| 13 |
*/ |
| 14 |
|
| 15 |
void setup() { |
| 16 |
// Open serial communications and wait for port to open: |
| 17 |
Serial.begin(9600); |
| 18 |
while (!Serial) { |
| 19 |
; // wait for serial port to connect. Needed for native USB port only |
| 20 |
} |
| 21 |
|
| 22 |
// send an intro: |
| 23 |
Serial.println("\n\nString length() and trim():"); |
| 24 |
Serial.println(); |
| 25 |
} |
| 26 |
|
| 27 |
void loop() { |
| 28 |
// here's a String with empty spaces at the end (called white space): |
| 29 |
String stringOne = "Hello! "; |
| 30 |
Serial.print(stringOne); |
| 31 |
Serial.print("<--- end of string. Length: "); |
| 32 |
Serial.println(stringOne.length()); |
| 33 |
|
| 34 |
// trim the white space off the string: |
| 35 |
stringOne.trim(); |
| 36 |
Serial.print(stringOne); |
| 37 |
Serial.print("<--- end of trimmed string. Length: "); |
| 38 |
Serial.println(stringOne.length()); |
| 39 |
|
| 40 |
// do nothing while true: |
| 41 |
while (true); |
| 42 |
} |