String length() and trim() Commands

You can get the length of a Strings using the length() command, or eliminate extra characters using the trim() command. This example shows you how to use both commands.

Hardware Required

  • Arduino or Genuino Board


There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open.

Arduino bb

image developed using Fritzing. For more circuit examples, see the Fritzing project page


trim() is useful for when you know there are extraneous whitespace characters on the beginning or the end of a String and you want to get rid of them. Whitespace refers to characters that take space but aren't seen. It includes the single space (ASCII 32), tab (ASCII 9), vertical tab (ASCII 11), form feed (ASCII 12), carriage return (ASCII 13), or newline (ASCII 10). The example below shows a String with whitespace, before and after trimming:

See Also

Last revision 2015/08/11 by SM