Skip to content

Instantly share code, notes, and snippets.

@divayprakash
Created June 23, 2016 08:33
Show Gist options
  • Save divayprakash/f6b61a5675a25d08c9dd6f68335d9ead to your computer and use it in GitHub Desktop.
Save divayprakash/f6b61a5675a25d08c9dd6f68335d9ead to your computer and use it in GitHub Desktop.
Remove whitespace (" ","\t","\v","\f","\r","\n") from String in Arduino
//REMOVE WHITESPACES FROM STRING IN ARDUINO
String message = "\n\tThis is one messed-up string \r\n";
message.trim();
/*
trim()
Description : Get a version of the String with any leading and trailing whitespace removed.
As of 1.0, trim() modifies the string in place rather than returning a new one.
Syntax : string.trim()
Parameters : string: a variable of type String
Returns : none
Explanation : 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).
*/
@Katheesh
Copy link

This code helped me. Thanks, man.

@Stredoslovak
Copy link

Ugh...
Kinda lame of me to write my own function to do this and then finding out that this already exists.
But thanks.

@galch
Copy link

galch commented Jan 24, 2023

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment