converting String to string

If I want to convert a String to a string, is there an available function? Can I just lift the characters out of a String, pop them into a character array, and add '\0' at the end?

Also, must I always declare a string as an array of characters of a fixed length, or is there a way to declare a variable-sized character array?

Thanks in advance for any help.

jrdoner: If I want to convert a String to a string, is there an available function? Can I just lift the characters out of a String, pop them into a character array, and add '\0' at the end?

Also, must I always declare a string as an array of characters of a fixed length, or is there a way to declare a variable-sized character array?

Thanks in advance for any help.

toCharArray()

In the Arduino environment, you have the String class (upper case ‘S’) and a C string (lower case ‘s’) available to you. An object of the String class can be “grown” dynamically. However, the downside is that String objects consume considerably more memory than their C string equivalents. Also, String manipulation can end up fragmenting memory. The String class does have a method, toCharArray(), which can make the conversion. The Arduino IDE does support malloc() and free(), but most programmers seem to avoid them:

Unless there is some strong reason for using Strings, I’d stick with C strings.

jrdoner: If I want to convert a String to a string, is there an available function? Can I just lift the characters out of a String, pop them into a character array, and add '\0' at the end?

The String class is actually using a char array (null-terminated C-string) and a char pointer internally and you can simply access the char pointer with a function call by reference to StrringObject.c_str()

Typically ".c_str()" is enough to deal with all the stanard-C string functions which expect a string-pointer as parameter.

Or do you want to copy the contents of a String to a nullterminated string, which would need nearly twice as much RAM for keeping the contents then?