{"id":20152,"date":"2024-02-01T21:18:59","date_gmt":"2024-02-01T21:18:59","guid":{"rendered":"https:\/\/wolles-elektronikkiste.de\/?p=20152"},"modified":"2025-10-30T20:08:16","modified_gmt":"2025-10-30T20:08:16","slug":"character-arrays-vs-strings","status":"publish","type":"post","link":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings","title":{"rendered":"Character Arrays vs. Strings"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">About this Post<\/h2>\n\n<p>If you work with character sequences in your programs, you can use character arrays and String objects. Beginners in particular tend to use String objects because they are more convenient than character arrays. However, character arrays are more resource-efficient and should therefore be preferred.<\/p>\n<p>In this article, I will discuss and compare the essential operations that you can apply to character arrays and String objects. You will see that character arrays are in no way inferior to string objects in terms of functionality.<\/p>\n<p>We will look at the following operations in detail:<\/p>\n\n<figure class=\"wp-block-table is-style-stripes has-small-font-size\"><table><thead><tr><th>Operation<\/th><th>Function (Character Arrays)<\/th><th>Function (String objects) <\/th><\/tr><\/thead><tbody><tr><td><a href=\"#size_length\">Determine size \/ length<\/a><\/td><td>sizeof() \/ strlen()<\/td><td>sizeof() \/ length()<\/td><\/tr><tr><td><a href=\"#concatenate\">Concatenate<\/a><\/td><td>strcat() \/ strncat()<\/td><td>Operator \u2032+=\u2032<\/td><\/tr><tr><td><a href=\"#duplicate\">Duplicate<\/a><\/td><td>strdup()<\/td><td>Operator \u2032=\u2032<\/td><\/tr><tr><td><a href=\"#upr_lwr_case\">Convert to upper\/lower case letters <\/a><\/td><td>strupr() \/ strlwr()<\/td><td>toUpperCase() \/ toLowerCase()<\/td><\/tr><tr><td><a href=\"#read_char_at\">Read characters by index<\/a><\/td><td>charArray[index]<\/td><td>charAt()<\/td><\/tr><tr><td><a href=\"#delete_replace_char\">Replace \/ delete characters<\/a><\/td><td>charArray[index] <\/td><td>setCharAt() \/ remove()<\/td><\/tr><tr><td><a href=\"#substrings\">Create substrings<\/a><\/td><td>memcpy() \/ strchr() \/ strrchr()<\/td><td>substring()<\/td><\/tr><tr><td><a href=\"#mince\">Split by character<\/a><\/td><td>strtok()<\/td><td>substring() &amp; indexOf()<\/td><\/tr><tr><td><a href=\"#search_chars\">Search characters<\/a><\/td><td>strchr() \/ strrchr() \/ strstr()<\/td><td>indexOf() \/ lastIndexOf()<\/td><\/tr><tr><td><a href=\"#compare\">Compare<\/a><\/td><td>strcmp()<\/td><td>equals() \/ equalsIgnoreCase() \/<br\/>compareTo() \/ Operator \u2032==\u2032<\/td><\/tr><tr><td><a href=\"#char_array_from_to_string\">Conversion Char Arrays \u2194 Strings<\/a><\/td><td>to String: Operator \u2032=\u2032<\/td><td>to Char Array: toCharArray() \/ <br\/>c_str()<\/td><\/tr><tr><td><a href=\"#numbers_to_char_arrays_and_strings\">Create from numbers<\/a><\/td><td>dtostrf() \/ sprintf()<\/td><td>String()<\/td><\/tr><tr><td><a href=\"#char_arrays_and_strings_to_numbers\">Convert into numbers<\/a> <\/td><td>atof() \/ atoI() \/ atol()<\/td><td>toInt() \/ toFloat()<\/td><\/tr><tr><td><a href=\"#reading_via_serial\">Read in via serial<\/a><\/td><td>readBytes() \/ readBytesUntil()<\/td><td>readString() \/ readStringUntil()<\/td><\/tr><tr><td><a href=\"#passing_to_functions\">Transfer to functions<\/a><\/td><td>&#8211;<\/td><td><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Functions covered in the article<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\">Nomenclature<\/h2>\n\n<p>When I talk about character arrays in this article, I am referring to arrays of the data type <code>char<\/code> that end with the NULL character <code>'\\0'<\/code> (also known as the NULL terminator). You can also create non-NULL-terminated character arrays. However, these are pure arrays, to which you cannot apply most of the functions discussed here. In this respect, the term &#8220;character array&#8221; is ambiguous, but I don&#8217;t want to add &#8220;NULL terminated&#8221; every time.<\/p>\n<p>I refer to the instances of the String class that you create with <code>String myString = \"....\"<\/code> or <code>String myString = String (....)<\/code> as String objects. String objects are usually simply called strings. However, this designation is even more ambiguous than the character arrays just discussed. The term &#8220;string&#8221; could simply be the generic term for String objects and character arrays. Or take a look at the Arduino reference <a href=\"https:\/\/www.arduino.cc\/reference\/de\/language\/variables\/data-types\/string\/\" target=\"_blank\" rel=\"noopener\">here<\/a>, where character arrays are dealt with under the heading &#8220;string&#8221;.<\/p>\n<p>To complete the confusion, there is a class called std::string in the C++ standard library, which is similar to the String class developed specifically for the Arduino environment. For these reasons, I will consistently use the term &#8220;String object&#8221; (with capital &#8220;S&#8221;) in this article. That is a bit unwieldy, but hopefully clear.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"size_length\">Length and Size &#8211; or: The Difference between Character Arrays and String Objects<\/h2>\n\n<p>In the first sketch, we define a character array and a String object. The length, i.e. the number of characters, and the size, i.e. the memory space occupied in bytes, are then determined.<\/p>\n<p>You can find out the length of a character array with:<\/p>\n<p><code>size_t length = strlen( myCharArray );<\/code><\/p>\n<p>You can find out the length of a String object with :<\/p>\n<p><code>size_t length = myString.length();<\/code><\/p>\n<p>You can determine the size of a character array or a string object as follows:<\/p>\n<p><code>size_t sizeInBytes = sizeof( myCharArrayOrMyString );<\/code><\/p>\n<p>For the less experienced: The nice thing about the data type <code>size_t<\/code> is that you don&#8217;t have to worry about the actual size of the return value. You are on the safe side, regardless of the microcontroller used.<\/p>\n<p>Now to the sketch:<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"length_vs_size.ino\" data-enlighter-title=\"length_vs_size.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray[] = \"Hello, I am a character array\";\n    \/\/ char myCharArray[50] = \"Hello, I am a character array\";\n    \/\/ char myCharArray[] = {'H','e','l','l','o',',',' ','I',' ','a','m',' ','a',' ','c','h','a','r','a','c','t','e','r',' ','a','r','r','a','y','\\0'};\n    String myString = \"Hi all, I am a string\";\n\n    Serial.println(myCharArray);\n    Serial.print(\"Size of myCharArray: \");\n    size_t charSize = sizeof(myCharArray);\n    Serial.println(charSize);\n\n    Serial.print(\"Length of myCharArray: \");\n    size_t charLength = strlen(myCharArray); \n    Serial.println(charLength);\n    Serial.println();\n\n    Serial.println(myString);\n    Serial.print(\"Size of myString: \");\n    size_t stringSize = sizeof(myString);\n    Serial.println(stringSize);\n    \n    Serial.print(\"Length of myString: \");\n    size_t stringLength = myString.length();\n    Serial.println(stringLength);\n }\n\n void loop(){}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>And here is the output I got using an Arduino Nano (ATmega328P):<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/len_vs_size.png\"><img loading=\"lazy\" decoding=\"async\" width=\"492\" height=\"128\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/len_vs_size.png\" alt=\"Output of length_vs_size.ino\" class=\"wp-image-19888\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/len_vs_size.png 492w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/len_vs_size-300x78.png 300w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/a><figcaption class=\"wp-element-caption\">Output of length_vs_size.ino<\/figcaption><\/figure>\n\n<h4 class=\"wp-block-heading\">Conclusions on the Character Array<\/h4>\n\n<p>As you can see, the length of the character array is 29. If you count, this is actually the number of characters. However, the size is 30 bytes, as the invisible NULL terminator <code>'\\0'<\/code> is appended at the end. The NULL terminator signals the end of the character array.<\/p>\n<p>If you swap the comment line 3 and uncomment line 4, the length of the character array remains the same, but the size changes to 50 bytes.<\/p>\n<p>The alternative definition of <code>myCharArray<\/code> in line 5 more complex, but makes it clearer that it is an array. With this notation, you must append the NULL terminator yourself. Or you can define the length explicitly and leave space for the NULL terminator, e.g.:<\/p>\n<p><code>char charArray[6] = {'H','e','l','l','o'};<\/code><\/p>\n\n<h4 class=\"wp-block-heading\">Conclusions on the String Object<\/h4>\n\n<p>Things get strange with the String object. The length correctly indicates the number of characters. But why is the size of the string object 6 bytes? Well, this is because the String object is stored in two parts. The actual character string (&#8220;string buffer&#8221;) is located in the heap. The 6 bytes in the stack are only a kind of table of contents. The pointer to the heap address of the string buffer, the capacity of the String object and its length are stored there.<\/p>\n<p>When using other microcontrollers, things may be different. For example, with an ESP32, a string buffer is only stored in the heap if the length of the character string exceeds 13 characters. The size of the Atring object in the stack is always 16 bytes.<\/p>\n<p>If you would like to know more about string objects in the stack and heap, you could read my post about <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/sram-management\" target=\"_blank\" rel=\"noopener\">SRAM management<\/a>. There I also discuss the notorious heap fragmentation, which is often cited as an argument against the use of String objects.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"concatenate\">Concatenating Character Arrays and String Objects<\/h2>\n\n<h4 class=\"wp-block-heading\">Concatenating Character Arrays<\/h4>\n\n<p>This is how you concatenate two character arrays:<\/p>\n<p><code>strcat( char* destination, char* source );<\/code><\/p>\n<p>The &#8220;source&#8221; character array is appended to the &#8220;destination&#8221; array. You must ensure that &#8220;destination&#8221; is large enough to accommodate &#8220;source&#8221;. If this is not the case, there is a risk that you will overwrite memory locations that could contain other variables. The consequences are unpredictable. The problem is that there is no error message when compiling. <\/p>\n<p>Alternatively, you can append the first x characters of &#8220;source&#8221; to &#8220;destination&#8221;:<\/p>\n<p><code>strncat( char* destination, char* source, size_t x);<\/code><\/p>\n\n<h4 class=\"wp-block-heading\">Why do we pass &#8220;char* charArray&#8221;?<\/h4>\n\n<p>The name of an array represents the pointer to the beginning of the array. So if you define a character array as follows:&nbsp; <code>char myCharArray[] = \"....\"<\/code>, then <code>myCharArray<\/code> is a pointer of the data type <code>char<\/code>. When passing to a function, only the pointer is passed and not the entire array. I will discuss the passing of character arrays and String objects in detail later.<\/p>\n\n<h4 class=\"wp-block-heading\">Concatenating String Objects<\/h4>\n\n<p>String objects are simply concatenated using the &#8220;+&#8221; operator:<\/p>\n<p><code>String destination += String source;<\/code><\/p>\n<p>The undeniably pleasant thing about String objects is that you (supposedly) don&#8217;t have to worry about their length. However, the extension of String objects can lead to <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/sram-management#heap_fragmentation\" target=\"_blank\" rel=\"noopener\">heap fragmentation<\/a>. To prevent this, you can reserve memory space for the string object:<\/p>\n<p><code>myString.reserve( unigned int size );<\/code><\/p>\n<p>Where <code>size<\/code> is the expected maximum length of your string object.<\/p>\n<p>Here is a small example sketch on the subject of concatenation:<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"concatenate.ino\" data-enlighter-title=\"concatenate.ino\">void setup(){\n   Serial.begin(9600);\n   char myCharArray1[13] = \"Hello\";\n   char myCharArray2[] = \" World!\";\n\n   strcat(myCharArray1, myCharArray2);\n   \/\/ strncat(myCharArray1, myCharArray2, 5);  \/\/ add the first 5 chars\n   Serial.println(myCharArray1);\n\n   String myString1 = \"Hello\";\n   String myString2 = \" Earth!\";\n\n   myString1 += myString2;\n\n   Serial.println(myString1);\n}\n\nvoid loop(){}<\/pre>\n<p>\n\n<p>The unsurprising output is:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/concatenate.png\"><img loading=\"lazy\" decoding=\"async\" width=\"514\" height=\"39\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/concatenate.png\" alt=\"Output of concatenate.ino\" class=\"wp-image-19890\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/concatenate.png 514w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/concatenate-300x23.png 300w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/a><figcaption class=\"wp-element-caption\">Output of concatenate.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"duplicate\">Duplicate<\/h2>\n\n<p>You can create a copy of a character array as follows:<\/p>\n<p><code>char* copy = strdup( const char* original );<\/code><\/p>\n<p>It is even easier with String objects:<\/p>\n<p><code>String copy = String original;<\/code><\/p>\n<p>Here is an example:<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"duplicate.ino\" data-enlighter-title=\"duplicate.ino\">void setup(){\n    Serial.begin(9600);\n    \n    char myCharArray[] = \"Hello, I am a character array\";\n    Serial.print(\"Original: \");\n    Serial.println(myCharArray);\n\n    char* myCharArrayCopy = strdup(myCharArray);\n    Serial.print(\"Copy: \");\n    Serial.println(myCharArrayCopy);\n    Serial.println();\n\n    String myString = \"Hi all, I am a string\";\n    Serial.print(\"Original: \");\n    Serial.println(myString);\n\n    String myStringCopy = myString;\n    Serial.print(\"Copy: \");\n    Serial.println(myStringCopy);    \n}\n\nvoid loop(){}<\/pre>\n<p>\n\n<p>And here, for the sake of completeness, the output:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/duplicate.png\"><img loading=\"lazy\" decoding=\"async\" width=\"419\" height=\"98\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/duplicate.png\" alt=\"Output duplicate.ino\" class=\"wp-image-19904\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/duplicate.png 419w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/duplicate-300x70.png 300w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/a><figcaption class=\"wp-element-caption\">Output duplicate.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"upr_lwr_case\">Converting to Upper or Lower Case Letters<\/h2>\n\n<p>There are also very convenient functions for converting character arrays and String objects into upper or lower case letters.<\/p>\n<p>You can convert a character array as follows:<\/p>\n<p><code>strupr( char* charArray );<\/code> \/\/ Upper case letters<\/p>\n<p><code>strlwr( char* charArray );<\/code> \/\/ Lower case letters<\/p>\n<p>Use the following functions for String objects:<\/p>\n<p><code>myString.toUpperCase();<\/code> or <code>myString.toLowerCase();<\/code><\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"upper_lower_case.ino\" data-enlighter-title=\"upper_lower_case.ino\">void setup(){\n    Serial.begin(9600);\n    \n    char myCharArray[] = \"Hello, I am a character array\";\n    Serial.print(\"Original: \");\n    Serial.println(myCharArray);\n   \n    Serial.print(\"Upper Case: \");\n    strupr(myCharArray);\n    Serial.println(myCharArray);\n   \n    Serial.print(\"Lower Case: \");\n    strlwr(myCharArray);\n    Serial.println(myCharArray);\n    \n    Serial.println();\n\n    String myString = \"Hi all, I am a string\";\n    Serial.print(\"Original: \");\n    Serial.println(myString);\n\n    Serial.print(\"Upper Case: \");\n    myString.toUpperCase();\n    Serial.println(myString);\n    \n    Serial.print(\"Lower Case: \");\n    myString.toLowerCase();\n    Serial.println(myString);\n}\n\nvoid loop(){}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>The output is:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/upper_lower_case.png\"><img loading=\"lazy\" decoding=\"async\" width=\"501\" height=\"132\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/upper_lower_case.png\" alt=\"Output upper_lower_case.ino\" class=\"wp-image-19906\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/upper_lower_case.png 501w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/upper_lower_case-300x79.png 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/a><figcaption class=\"wp-element-caption\">Output upper_lower_case.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"read_char_at\">Reading a character by index<\/h2>\n\n<p>I don&#8217;t really need to explain how to read a character at a specific position in the character array:<\/p>\n<p><code>char charAtIndexI = charArray[i];<\/code><\/p>\n<p>There is a separate function for String objects for this purpose:<\/p>\n<p><code>char charAtIndexI = myString.charAt(i);<\/code><\/p>\n<p>Here&#8217;s an example:<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"read_char_at_index_i.ino\" data-enlighter-title=\"read_char_at_index_i.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray[] = \"Hello, I am a character array!\";\n\n    Serial.print(\"Tenth character: \");\n    char charAtIndex10 = myCharArray[10];\n    Serial.println(charAtIndex10);\n\n    String myString = \"Hi all, I am a string\";\n    \n    Serial.print(\"Tenth character: \");\n    charAtIndex10 = myString.charAt(10);\n    Serial.println(charAtIndex10);\n }\n\n void loop(){}<\/pre>\n<p>\n\n<p>The sketch gives the following output:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/access_character.png\"><img loading=\"lazy\" decoding=\"async\" width=\"549\" height=\"40\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/access_character.png\" alt=\"Output read_char_at_index_i.ino\" class=\"wp-image-19893\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/access_character.png 549w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/access_character-300x22.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/access_character-542x40.png 542w\" sizes=\"auto, (max-width: 549px) 100vw, 549px\" \/><\/a><figcaption class=\"wp-element-caption\">Output read_char_at_index_i.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"delete_replace_char\">Replacing and Deleting Characters<\/h2>\n\n<p>In a character array, you replace characters by accessing them via the index:<\/p>\n<p><code>charArray[index] = 'x'; \/\/ replace x by any other character<\/code><\/p>\n<p>To delete characters, you have to &#8220;tinker&#8221; a little. You let the remaining characters move to the left after the characters to be deleted, and then attach the NULL terminator.<\/p>\n<p>String objects have their own functions for replacing and deleting characters:<\/p>\n<p><code>myString.setCharAt[index] = 'x';<\/code><\/p>\n<p><code>myString.remove( index, length );<\/code><\/p>\n<p>Example sketch:<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"replace_remove.ino\" data-enlighter-title=\"replace_remove.ino\">void setup(){\n    Serial.begin(9600);\n    \n    char myCharArray[] = \"abcdefghijklm\";\n    Serial.print(\"Original: \");\n    Serial.println(myCharArray);\n\n    myCharArray[3] = 'c';\n    Serial.print(\"\\\"d\\\" replaced by \\\"c\\\": \");\n    Serial.println(myCharArray);\n\n    remove(myCharArray, sizeof(myCharArray), 3, 5);\n    Serial.print(\"Removed 5 chars from index 3: \");\n    Serial.println(myCharArray);\n    Serial.println();\n\n    String myString = \"nopqrstuvwxyz\";\n    Serial.print(\"Original: \");\n    Serial.println(myString);\n    \n    myString.setCharAt(3, 'p');\n    Serial.print(\"\\\"q\\\" replaced by \\\"p\\\": \");\n    Serial.println(myString);\n\n    myString.remove(3, 5);\n    Serial.print(\"Removed 5 chars from index 3: \");\n    Serial.println(myString);    \n}\n\nvoid loop(){}\n\nvoid remove(char *cArr, size_t size, size_t index, size_t len){  \/\/ remove for char arrays\n    for(size_t i = index; i &lt; (size-len); i++){\n        cArr[i] = cArr[i+len];\n    }\n    cArr[size-len] = '\\0';\n}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>The output is as expected:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/replace_remove.png\"><img loading=\"lazy\" decoding=\"async\" width=\"439\" height=\"133\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/replace_remove.png\" alt=\"Output replace_remove.ino\" class=\"wp-image-19911\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/replace_remove.png 439w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/replace_remove-300x91.png 300w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/a><figcaption class=\"wp-element-caption\">Output replace_remove.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"substrings\">Substrings<\/h2>\n\n<h4 class=\"wp-block-heading\">Sections of character arrays starting from a certain index<\/h4>\n\n<p>Creating substrings from character strings is a bit awkward. We use the  function <code>memcpy()<\/code> for this. In general terms, the function is used as follows:<\/p>\n<p><code>void* memcpy ( void* destination, const void* source, size_t num );<\/code><\/p>\n<p>&#8220;Translated&#8221;, this means: <code>memcpy()<\/code> copies <code>num<\/code> bytes, starting at the address to which the pointer <code>source<\/code> points, and copies them to the address to which the pointer <code>destination<\/code> points. The data type <code>void*<\/code> makes <code>memcpy()<\/code> versatile. It does not matter which data type the pointers to be passed point to. To create a substring, we use the function as follows:<\/p>\n<p><code>memcpy( subCharArray, charArray + index, length );<\/code><\/p>\n<p>When you specify the length of <code>subCharArray<\/code>, please consider the NULL terminator. Regardless of this, you must actually attach the NULL terminator. Or, even easier, you initialize <code>subCharArray<\/code> as <code>{'\\0'}<\/code>. As a result, all elements of the array are initialized as NULL characters.<\/p>\n\n<h4 class=\"wp-block-heading\">Substrings of character arrays beginning from a certain character<\/h4>\n\n<p>If you want to create a substrings that begins at a specific character of the original array and captures all characters from there to the end, then the function <code>strchr()<\/code> comes into consideration. The following line of code defines the pointer <code>subCharArray<\/code>, which points to the address at which the character <code>charToBeFound<\/code> appears for the first time in <code>charArray<\/code>:<\/p>\n<p><code>char* subCharArray = strchr(const char* charArray, char charToBeFound);<\/code><\/p>\n<p>Alternatively, you can use <code>strrchr()<\/code> (with two &#8220;r &#8220;s). The function returns a pointer that points to the last occurrence of the character you are looking for.<\/p>\n<p>You just need to be aware that <code>subCharArray<\/code> is still part of <code>charArray<\/code>. Changes to <code>subCharArray<\/code> also change <code>charArray<\/code>. If you do not want to do this, you should create a copy with <code>strdup()<\/code> or via <code>memcpy()<\/code>.<\/p>\n\n<h4 class=\"wp-block-heading\">Substrings from String Objects<\/h4>\n\n<p>For String objects, there is the extremely convenient function <code>substring()<\/code> available. You can use it to create a substring that starts at a specific index. If you only pass the index, all other characters are included in the substring until the end. Alternatively, you can pass a second index <em>before<\/em> which the substring ends.<\/p>\n<p><code>String subString = myString.subtring( index );<\/code><\/p>\n<p><code>String subString = myString.substring( startIndex, endIndex ); \/\/ substring from startIndex to endIndex - 1<\/code><\/p>\n<p>Here is the example:<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"substrings.ino\" data-enlighter-title=\"substrings.ino\">void setup(){\n    Serial.begin(9600);\n    \n    char myCharArray[] = \"abcdefghijklm\";\n    Serial.print(\"Original: \");\n    Serial.println(myCharArray);\n\n    char mySubCharArray[6] = {'\\0'};\n    Serial.print(\"Substring from index 3 to 7: \");\n    memcpy(mySubCharArray, myCharArray+3, 5);\n    Serial.println(mySubCharArray);\n\n    char mySubCharArray_2[6] = {'\\0'};\n    Serial.print(\"Substring 3-7, alternative: \");\n    subArray(mySubCharArray_2, myCharArray, 3, 5);\n    Serial.println(mySubCharArray_2);\n    \n    char *mySubCharArray_3 = strchr(myCharArray, 'i');  \/\/ strchr(): first occurance \/ strrchr(): finds last occurance\n    Serial.print(\"Substring, beginning at \\\"i\\\": \");\n    Serial.println(mySubCharArray_3);\n    Serial.println();\n       \n    String myString = \"nopqrstuvwxyz\";\n    Serial.print(\"Original: \");\n    Serial.println(myString);\n    \n    String mySubString = myString.substring(3, 8);\n    Serial.print(\"Substring from index 3 to 7: \");\n    Serial.println(mySubString);   \n\n    String mySubString_2 = myString.substring(7);\n    Serial.print(\"Substring from index 7: \");\n    Serial.println(mySubString_2);   \n}\n\nvoid loop(){}\n\nvoid subArray(char* destination, char* source, size_t index, size_t len){\n    memcpy(destination, source + index, len);\n}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>The sketch generates the following output:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/substrings.png\"><img loading=\"lazy\" decoding=\"async\" width=\"371\" height=\"146\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/substrings.png\" alt=\"Output substrings.ino\" class=\"wp-image-19915\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/substrings.png 371w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/substrings-300x118.png 300w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><\/a><figcaption class=\"wp-element-caption\">Output substrings.ino<\/figcaption><\/figure>\n\n<h4 class=\"wp-block-heading\" id=\"mince\">Complete Splitting of the Character Sequence by Specific Characters<\/h4>\n\n<p>You may want to split character arrays and String objects by certain characters. This could be necessary, for example, if a character sequence contains several measured values that are separated from each other by a specific character (delimiter).<\/p>\n<p>Here, the character arrays offer the simpler solution:<\/p>\n<p><code>char* chunk = strtok( char* charArray, char delimiter );<\/code><\/p>\n<p><code>chunk<\/code> is a character array that contains all characters up to the first occurrence of the delimiter. The pleasant but perhaps also confusing thing is that you pass the NULL pointer to extract the next section:<\/p>\n<p><code>char* nextChunk = strtok( NULL, char delimiter );<\/code><\/p>\n<p>This works because <code>strtok()<\/code> remembers the address of the character array and the index of the last delimiter. If no more delimiters are found, <code>strtok()<\/code> returns the NULL pointer.<\/p>\n<p>You can split string objects according to delimiters by searching for the delimiters with <code>indexOf()<\/code> and creating the sections with <code>substring()<\/code>.<\/p>\n<p>Here is the example of this:<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"mince_char_arrays_and_strings.ino\" data-enlighter-title=\"mince_char_arrays_and_strings.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray[] = \"Hello, I am a character array\";\n    String myString = \"Hi all, I am a string\";\n\n    char *cArrChunk = strtok(myCharArray, \" \");\n    while(cArrChunk != NULL){\n        Serial.println(cArrChunk);\n        cArrChunk = strtok(NULL, \" \");\n    }\n    Serial.print(\"myCharArray: \");\n    Serial.println(myCharArray);\n    Serial.println();\n\n    String stringChunk = myString.substring(0, myString.indexOf(\" \"));\n    while(myString != \"\"){\n        Serial.println(stringChunk);\n        myString.remove(0, stringChunk.length()+1);\n        stringChunk = myString.substring(0, myString.indexOf(\" \"));\n    }\n    Serial.print(\"myString: \");\n    Serial.println(myString);\n}\n\nvoid loop(){}<\/pre>\n<p>\n\n<p>Here is the output:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/mince.png\"><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"144\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/mince.png\" alt=\"Output mince_char_arrays_and_strings.ino\" class=\"wp-image-19950\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/mince.png 527w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/mince-300x82.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/a><figcaption class=\"wp-element-caption\">Output mince_char_arrays_and_strings.ino<\/figcaption><\/figure>\n\n<p>As you can see, <code>myCharArray<\/code> and <code>myString<\/code> are modified by this procedure. If you don&#8217;t want that, you&#8217;ll have to work with copies.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"search_chars\">Search characters<\/h2>\n\n<p>We have already searched for specific characters before, but I want to come back to this topic again, as we have omitted some aspects so far. This also includes determining the index of a specific character in a character array. The determination of the index for the last occurrence of a character in a string object is also missing.&nbsp;<\/p>\n\n<h4 class=\"wp-block-heading\">Searching Characters in a Character Array<\/h4>\n\n<p>As we have seen, <code>strchr()<\/code> returns the pointer to the address where the character we are searching is located for the first time. <code>strrchr()<\/code> provides the pointer to the last address where the searched character is located. To calculate the index from these pointers, simply subtract the pointer to the character array:<\/p>\n<p><code>indexOfCharX = strchr( char* charArray, char charX ) - charArray;<\/code><\/p>\n<p>Alternatively, you can, of course, compare every character in your array with the character you are searching for. But that is less elegant.<\/p>\n<p>If you are not searching for a character but for a character sequence within the character array, you can use the <code>strstr()<\/code> function:<\/p>\n<p><code>char* ptrToFirstString = strstr( char* charArray, char* searchString );<\/code><\/p>\n<p>If <code>strchr()<\/code>, <code>strrchr()<\/code> and <code>strstr()<\/code> end their search unsuccessfully, they return the NULL pointer.<\/p>\n\n<h4 class=\"wp-block-heading\">Searching Characters in a String Object <\/h4>\n\n<p>As we have seen, we can find out the index for the first occurrence of a character in a String object using <code>indexOf()<\/code>. However, the function also accepts character arrays and String objects:<\/p>\n<p><code>int indexOfFirstStringX = myString.indexOf( char* \/ String stringX);<\/code><\/p>\n<p>You can find the index for the last occurrence of one or more characters with :<\/p>\n<p><code>int indexOfLastStringX = myString.lastIndexOf( char* \/ String stringX );<\/code><\/p>\n<p>You can then determine the index for the first or last occurrence of a character or character string from a certain index:<\/p>\n<p><code>int indexOfFirstStringXAfterIndexY = myString.indexOf( char* \/ String stringX, size_t indexY);<\/code><\/p>\n<p><code>int indexOfLastStringXAfterIndexY = myString.lastIndexOf( char* \/ String stringX, size_t indexY);<\/code><\/p>\n<p>If <code>indexOf()<\/code> and <code>lastIndexOf()<\/code> end their search unsuccessfully, they return -1.<\/p>\n<p>Example sketch:<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"find_characters.ino\" data-enlighter-title=\"find_characters_in_char_arrays.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray[] = \"I am a character array!\";\n\n    Serial.print(\"Rest of myCharArray beginning at first \\\"a\\\": \");\n    char *ptrToFirstA = strchr(myCharArray, 'a');\n    if (ptrToFirstA != NULL){\n        Serial.println(ptrToFirstA);\n    }\n    else{\n        Serial.println(\"Not found\");\n    }\n    \n    Serial.print(\"Rest of myCharArray beginning at first \\\"x\\\": \");\n    char *ptrToFirstX = strchr(myCharArray, 'x');\n    if (ptrToFirstX != NULL){\n        Serial.println(ptrToFirstX);\n    }\n    else{\n        Serial.println(\"Not found\");\n    }\n\n    Serial.print(\"Rest of myCharArray beginning at last \\\"a\\\": \");\n    char *ptrToLastA = strrchr(myCharArray, 'a');\n    Serial.println(ptrToLastA);\n    \n    Serial.print(\"Last \\\"a\\\" is at position: \");\n    int indexOfLastA = findLastIndexOf(myCharArray, sizeof(myCharArray), 'a'); \n    Serial.println(indexOfLastA);\n\n    Serial.print(\"Alternative Method: Last \\\"a\\\" position: \");\n    indexOfLastA = ptrToLastA - myCharArray;\n    Serial.println(indexOfLastA);\n\n    Serial.print(\"First \\\"ar\\\" position: \");\n    char *ptrToFirstAR = strstr(myCharArray, \"ar\");\n    int indexOfFirstAR = ptrToFirstAR - myCharArray;\n    Serial.println(indexOfFirstAR);\n}\n\nvoid loop(){}\n\nsize_t findLastIndexOf(char *cArr, size_t size, char searchChar){\n    size_t lastPos = -1;\n    for(size_t i=0; i&lt;size; i++){\n        if(cArr[i] == searchChar){\n            lastPos = i;\n        }\n    }\n    return lastPos;\n}<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"find_characters.ino\" data-enlighter-title=\"find_characters_in_strings.ino\">void setup(){\n    Serial.begin(9600);\n    String myString = \"I am not a character array but a String\";\n\n    Serial.print(\"First index of \\\"t\\\": \");\n    int indexOfFirstT = myString.indexOf(\"t\");\n    Serial.println(indexOfFirstT);\n\n    Serial.print(\"First index of \\\"t\\\" after pos. 7: \");\n    int indexOfFirstTAfterPos7 = myString.indexOf(\"t\", 8);\n    Serial.println(indexOfFirstTAfterPos7);\n\n    Serial.print(\"First index of \\\"x\\\": \");\n    size_t indexOfFirstX = myString.indexOf(\"x\");\n    if((int)indexOfFirstX != -1){\n        Serial.println(indexOfFirstX);\n    }\n    else{\n        Serial.println(\"Not found\");\n    }\n\n    Serial.print(\"Last index of \\\"t\\\": \");\n    int indexOfLastT = myString.lastIndexOf(\"t\");\n    Serial.println(indexOfLastT);\n\n    Serial.print(\"Last index of \\\"ar\\\": \");\n    char searchString[] = \"ar\";\n    \/\/String searchString = \"ar\";\n    int indexOfLastAR = myString.lastIndexOf(searchString);\n    Serial.println(indexOfLastAR);\n}\n\nvoid loop(){}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>And here are the outputs:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/find_characters-2.webp\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"125\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/find_characters-2.webp\" alt=\"Output find_characters.ino, left: character array, right: String object\" class=\"wp-image-19933\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/find_characters-2.webp 883w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/find_characters-2-300x42.webp 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/find_characters-2-768x109.webp 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/a><figcaption class=\"wp-element-caption\">Output find_characters.ino, left: character array, right: String Object<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"compare\">Compare<\/h2>\n\n<h4 class=\"wp-block-heading\">Comparing Character Arrays<\/h4>\n\n<p>This is how you compare two character arrays:<\/p>\n<p><code>int result = strcmp( char* charArray1, char* charArray2 );<\/code><\/p>\n<p>If you only want to compare the first n characters of the two arrays, then pass &#8220;n&#8221; as the third parameter:<\/p>\n<p><code>int result = strcmp( char* charArray_1, char* charArray_2, size_t n );<\/code><\/p>\n<p>The <code>strcmp()<\/code> function compares the arrays character by character starting at index 0. This continues until it encounters the first different character or reaches the end or the last of the characters to be compared. If the function encounters a different character, <code>result<\/code> corresponds to the difference between the ASCII codes of these characters. If <code>result<\/code> is negative, then <code>charArray_1<\/code> contains the character with the lower ASCII code. If <code>result<\/code> is positive, then it is the other way around.&nbsp; If <code>result<\/code> is 0, then the two character arrays are identical, or more precisely: they have an identical character sequence.<\/p>\n<p>If you want to perform the comparison regardless of upper or lower case letters, you can convert the character arrays with <code>strupr()<\/code> or <code>strlwr()<\/code> before the comparison. However, this will permanently change them, so you may need to make copies.<\/p>\n\n<h4 class=\"wp-block-heading\">Comparing String Objects <\/h4>\n\n<p>To check whether two String objects are the same, use the function <code>equals()<\/code>:<\/p>\n<p><code>bool result = myString.equals( myString_2 );<\/code><\/p>\n<p>If you only want to check parts of <code>myString_2<\/code> and <code>myString<\/code> for identity, you must combine <code>equals()<\/code> with <code>substring()<\/code>. You can ignore upper and lower case with the variant <code>equalsIgnoreCase()<\/code>:<\/p>\n<p><code>bool result = myString.equalsInoreCase( myString_2 );<\/code><\/p>\n<p>As an alternative to <code>equals()<\/code>, use the function <code>compareTo()<\/code>:<\/p>\n<p><code>int result = myString.compareTo( myString_2 );<\/code><\/p>\n<p>Just like <code>strcmp()<\/code>, <code>compareTo()<\/code> compares the two String objects character by character. If two characters are different, the function returns the difference between the ASCII codes of these characters.<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"compare.ino\" data-enlighter-title=\"compare_char_arrays.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray1[] = \"I am a character array!\";\n    char myCharArray2[] = \"I am also a character array!\";\n    char myCharArray3[] = \"I am a character array!\";\n    char myCharArray4[] = \"I aM A cHaRaCteR aRrAy!\";\n    char myCharArray5[] = \"abcd\";\n    char myCharArray6[] = \"aacd\";\n  \n\n    Serial.print(\"Comparing 1, 2: \");\n    int result = strcmp(myCharArray1, myCharArray2);\n    Serial.println(result);\n\n    Serial.print(\"Comparing 1, 2 (first 6 characters): \");\n    result = strncmp(myCharArray1, myCharArray2, 6);\n    Serial.println(result);\n\n    Serial.print(\"Comparing 1, 3: \");\n    result = strcmp(myCharArray1, myCharArray3);\n    Serial.println(result);\n\n    Serial.print(\"Comparing 1, 4: \");\n    result = strcmp(myCharArray1, myCharArray4);\n    Serial.println(result);\n\n    Serial.print(\"Comparing 1, 4, non-case-sensitive: \");\n    result = strcmp(strlwr(myCharArray1), strlwr(myCharArray3)); \/\/ changes the char arrays permanently!\n    Serial.println(result); \n    Serial.println(myCharArray1);\n\n    Serial.print(\"Comparing 5, 6: \");\n    result = strcmp(myCharArray5, myCharArray6);\n    Serial.println(result);\n\n    Serial.print(\"Comparing 6, 5: \");\n    result = strcmp(myCharArray6, myCharArray5);\n    Serial.println(result);\n }\n\nvoid loop(){}<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"compare.ino\" data-enlighter-title=\"compare_strings.ino\">void setup(){\n   Serial.begin(9600);\n   String myString1 = \"I am a String!\";\n   String myString2 = \"I am also a String!\";\n   String myString3 = \"I am a String!\";\n   String myString4 = \"I aM A sTrInG!\";\n   String myString5 = \"abcd\";\n   String myString6 = \"aacd\";\n \n\n   Serial.print(\"Comparing 1, 2: \");\n   int result = (myString1.equals(myString2));\n   Serial.println(result);\n\n   Serial.print(\"Comparing 1, 2 (first 6 characters): \");\n   result = (myString1.substring(0,6)).equals(myString2.substring(0,6));\n   Serial.println(result);\n  \n   Serial.print(\"Comparing 1, 3: \");\n   result = myString1.equals(myString3);\n   Serial.println(result);\n\n   Serial.print(\"Comparing 1, 4: \");\n   result = myString1.equals(myString4);\n   Serial.println(result);\n\n   Serial.print(\"Comparing 1, 4, non-case-sensitive: \");\n   result = myString1.equalsIgnoreCase(myString4);\n   Serial.println(result);\n   Serial.println(myString1);\n\n   Serial.print(\"Comparing 5, 6: \");\n   result = myString5.compareTo(myString6);\n   Serial.println(result);\n\n   Serial.print(\"Comparing 6, 5: \");\n   result = myString6.compareTo(myString5);\n   Serial.println(result);\n}\n\nvoid loop(){}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>The outputs are:<\/p>\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare.webp\"><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"168\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare.webp\" alt=\"left: output compare_char_arrays.ino, right: compare_strings.ino\" class=\"wp-image-20010\" style=\"width:840px;height:auto\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare.webp 738w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare-300x68.webp 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/a><figcaption class=\"wp-element-caption\">left: output compare_char_arrays.ino, right: compare_strings.ino<\/figcaption><\/figure>\n\n<h4 class=\"wp-block-heading\">Using the &#8220;==&#8221; operator<\/h4>\n\n<p>If you use the operator <code>==<\/code> on two character arrays, it compares two pointers. You only get a <code>true<\/code> if both pointers point to the same address, i.e., if the two character arrays are not only the same but truly identical. <\/p>\n<p>With String objects, on the other hand, the operator <code>==<\/code> checks whether the &#8220;content&#8221;, i.e. the string buffers, are the same. <code>equals()<\/code> and <code>==<\/code> are equivalent. <\/p>\n<p>Here is an example sketch:<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"compare_2.ino\" data-enlighter-title=\"compare_2.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray1[] = \"I am a character array!\";\n    char myCharArray2[] = \"I am a character array!\";\n    char* myCharArray3 = myCharArray2;\n    String myString1 = \"I am a String!\";\n    String myString2 = \"I am a String!\";\n\n    Serial.print(\"Char array comparison 1: \");\n    if(myCharArray1 == myCharArray2){   \/\/ Compares the pointers, not the arrays\n        Serial.println(\"Identical\");\n    }\n    else{\n        Serial.println(\"Different\");\n    }\n\n    Serial.print(\"Char array comparison 2: \");\n    if(myCharArray2 == myCharArray3){   \/\/ Compares the pointers, not the arrays\n        Serial.println(\"Identical\");\n    }\n    else{\n        Serial.println(\"Different\");\n    }\n\n    Serial.print(\"Char array comparison 3: \");\n    if(myCharArray1 == \"I am a character array!\"){  \/\/ Don't do this!!!!\n        Serial.println(\"Identical\");\n    }\n    else{\n        Serial.println(\"Different\");\n    }\n\n    Serial.print(\"String comparison 1: \");\n    if(myString1 == myString2){\n        Serial.println(\"Equal\");\n    }\n    else{\n        Serial.println(\"Different\");\n    }\n\n    Serial.print(\"String comparison 2: \");\n    if(myString1 == \"I am a String!\"){\n        Serial.println(\"Equal\");\n    }\n    else{\n        Serial.println(\"Different\");\n    }\n }\n\n void loop(){}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>Here is the output for compare_2.ino:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare_2-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"387\" height=\"91\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare_2-1.png\" alt=\"Output compare_2.ino\" class=\"wp-image-20017\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare_2-1.png 387w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/compare_2-1-300x71.png 300w\" sizes=\"auto, (max-width: 387px) 100vw, 387px\" \/><\/a><figcaption class=\"wp-element-caption\">Output compare_2.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"char_array_from_to_string\">Creating Character Arrays from String Objects and vice versa<\/h2>\n\n<p>Creating a String object from a character array is simple:<\/p>\n<p><code>String myString = ( const char* myCharArray );<\/code><\/p>\n<p>There are two functions you can use to create character arrays from string objects:<\/p>\n<p><code>myString.toCharArray( char* myCharArray, size_t length );<\/code><\/p>\n<p><code>const char* myCharArray = myString.c_str();<\/code><\/p>\n<p>Use the function <code>toCharArray()<\/code> to copy the string buffer, i.e. the character sequence, into a previously defined character array. The first parameter to be passed to <code>toCharArray()<\/code> is the character array, the second is the length of the character array. The NULL terminator is attached automatically. You must take this into account when defining the length.<\/p>\n<p>The function <code>c_str()<\/code> does something entirely different by returning the pointer to the string buffer in the heap (i.e. where the content of the String object is stored). In other words, nothing new is created here, but you access the character sequence of the String object directly. In this respect, it makes sense for the return value to be a constant. This protects you from accidentally changing your string object. If you intend to change the character array, it is best to create a copy with <code>memcpy()<\/code> (see example sketch). But then you could just as well use <code>toCharArray()<\/code>.<\/p>\n<p>So why do we need <code>c_str()<\/code>? This function is always useful if you want to pass on the content of the String object unchanged to another function. The <code>c_str()<\/code> function is much faster and saves SRAM.<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"char_array_to_string_and_string_to_char_array.ino\" data-enlighter-title=\"char_array_to_string_and_string_to_char_array.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray[] = \"I was born as a character array!\";\n\n    String stringFromCharArray = String(myCharArray);\n    Serial.println(stringFromCharArray);\n\n    String myString = \"I was born as a String\";\n\n    char charArrayFromString[myString.length() + 1];\n    myString.toCharArray(charArrayFromString, myString.length() + 1);\n    Serial.println(charArrayFromString);\n\n    char charArrayFromString2[myString.length() + 1];\n    memcpy(charArrayFromString2, myString.c_str(), myString.length() + 1);\n    Serial.println(charArrayFromString2);\n}\n\nvoid loop(){}<\/pre>\n<p>\n\n<p>And here is the unsurprising output:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_to_string_and_string_to_char_array.png\"><img loading=\"lazy\" decoding=\"async\" width=\"353\" height=\"59\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_to_string_and_string_to_char_array.png\" alt=\"Output char_array_to_string_and_string_to_char_array.ino\" class=\"wp-image-20026\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_to_string_and_string_to_char_array.png 353w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_to_string_and_string_to_char_array-300x50.png 300w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/a><figcaption class=\"wp-element-caption\">Output char_array_to_string_and_string_to_char_array.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"numbers_to_char_arrays_and_strings\">Converting Numbers to Character Arrays and String Objects<\/h2>\n\n<h4 class=\"wp-block-heading\">Decimal Numbers into Character Arrays <\/h4>\n\n<p>Floating-point numbers (double and float) and integers (int, long) can be easily converted into character arrays using the <code>dtostrf()<\/code> function:<\/p>\n<p><code>dtostrf( double myDouble, signed char min_width, unsigned char precision, char* myCharArray );<\/code><\/p>\n<p>The parameters are:<\/p>\n<ul>\n<li><strong>myDouble<\/strong>: The number to be converted. Alternative data types: float, int, long.<\/li>\n<li><strong>min_witdth<\/strong>: Minimum width of the output. If your converted number is shorter, it will be padded with spaces and shifted to the right accordingly.<\/li>\n<li><strong>precision<\/strong>: The number of decimal places. If you convert integers, the decimal places are zeros.<\/li>\n<li><strong>myCharArray<\/strong>: The &#8220;target&#8221; character array.<\/li>\n<\/ul>\n<p>You have to make sure that the character array is large enough.<\/p>\n\n<h4 class=\"wp-block-heading\">Converting other Number systems into Character Arrays<\/h4>\n\n<p>If you want to convert a number in the hexadecimal system into a character array, I recommend using the function <code>sprintf()<\/code>. I&#8217;ll show you how to do this in the example sketch below. Since you could write an entire article just about <code>sprintf()<\/code> (which I will do at some point), I will refrain from further explanations. A documentation of the <code>sprintf()<\/code> function can be found <a href=\"https:\/\/cplusplus.com\/reference\/cstdio\/sprintf\/\" target=\"_blank\" rel=\"noopener\">here<\/a>, a documentation of the underlying <code>printf()<\/code> function can be found <a href=\"https:\/\/cplusplus.com\/reference\/cstdio\/printf\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>There is no ready-made function for displaying numbers in the binary system as a character array. For the example sketch, I wrote the function <code>toBinary()<\/code>. It goes through the bits of the number to be converted one by one and checks which of the bits are set. All preceding zeros are truncated. I also wrote a second version. The number to be converted is shifted bit by bit to the left, and it is always checked whether the most significant bit (&#8220;msb&#8221;) is set.<\/p>\n\n<h4 class=\"wp-block-heading\">Converting Numbers into String Objects<\/h4>\n\n<p>Converting numbers into string objects is much easier. The following applies to integers:<\/p>\n<p><code>String myString = String( int myInt, base );  \/\/ base: BIN, HEX, DEC (optional)<\/code><\/p>\n<p>Instead of integer values, you can also pass numbers of the type <code>byte<\/code>, <code>long<\/code> or the <code>unsigned<\/code> variants.<\/p>\n<p>For floating-point numbers, use (in this example <code>float<\/code>):<\/p>\n<p><code>String myString = String( float myFloat, precision ); \/\/ prescision: optional<\/code><\/p>\n<p>The parameter <code>precision<\/code> specifies the number of decimal places. If you omit this parameter, two decimal places will be used for the String object.<\/p>\n<p>Here are a few examples:<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"floats_integers_to_char_arrays_and_strings.ino\" data-enlighter-title=\"floats_integers_to_char_arrays_and_strings.ino\">void setup(){\n    Serial.begin(9600);\n    int myInt = 4242;\n    float myFloat = -123.4567;\n    \n    Serial.println(\"Converted to character arrays: \");\n    char myCharArray_1[10];\n    dtostrf(myFloat, 8, 4, myCharArray_1);\n    Serial.print(\"myFloat, version 1: \");\n    Serial.println(myCharArray_1);\n\n    char myCharArray_2[14];\n    dtostrf(myFloat, 13, 2, myCharArray_2);\n    Serial.print(\"myFloat, version 2: \");\n    Serial.println(myCharArray_2);\n\n    char myCharArray_3[14];\n    dtostrf(myInt, 13, 0, myCharArray_3);\n    Serial.print(\"myInt             : \");\n    Serial.println(myCharArray_3);\n\n    char myCharArray_4[17];\n    toBinary(myInt, myCharArray_4);\n    Serial.print(\"myInt, binary     : \");\n    Serial.println(myCharArray_4);\n   \n    char myCharArray_5[5];\n    sprintf(myCharArray_5,\"%X\", myInt);\n    Serial.print(\"myInt, hexadecimal: \");\n    Serial.println(myCharArray_5);\n    Serial.println();\n\n\n    Serial.println(\"Converted to strings: \");\n    String myString_1 = String(myFloat,4);\n    Serial.print(\"myFloat, version 1: \");\n    Serial.println(myString_1);\n\n    String myString_2 = \"     \" + String(myFloat);\n    Serial.print(\"myFloat, version 2: \");\n    Serial.println(myString_2);\n\n    String myString_3 = \"        \" + String(myInt);\n    Serial.print(\"myInt             : \");\n    Serial.println(myString_3);\n\n    String myString_4 = String(myInt, BIN);\n    Serial.print(\"myInt, binary     : \");\n    Serial.println(myString_4);\n\n    String myString_5 = String(myInt, HEX);\n    Serial.print(\"myInt, hexadecimal: \");\n    Serial.println(myString_5);\n}\n\nvoid loop(){}\n\nvoid toBinary(uint16_t intVal, char *cArr){\n    int bitNo = 15;\n    int index = 0;\n    \n    int isBitSet = intVal &amp; bit(bitNo);\n    while(!isBitSet &amp;&amp; (bitNo &gt; 0)){\n        bitNo--;\n        isBitSet = intVal &amp; bit(bitNo);\n    }\n    while(bitNo&gt;=0){\n        if(isBitSet){\n            cArr[index] = '1';\n        }\n        else {\n            cArr[index] = '0';\n        }\n        index++;\n        bitNo--;\n        isBitSet = intVal &amp; bit(bitNo);\n    }\n    cArr[index] = '\\0';  \n}\n\n\/* Alternative way *\/\n\/\/ void toBinary(uint16_t intVal, char *cArr){\n\/\/     int bitNo = 15;\n\/\/     int index = 0;\n    \n\/\/     int msb = intVal &amp; 0x8000;\n\/\/     while(!msb &amp;&amp; (bitNo &gt; 0)){\n\/\/         bitNo--;\n\/\/         intVal = intVal &lt;&lt; 1;\n\/\/         msb = intVal &amp; 0x8000;  \n\/\/     }\n\/\/     while(bitNo&gt;=0){\n\/\/         msb = intVal &amp; 0x8000;\n\/\/         intVal = intVal &lt;&lt; 1;\n\/\/         if(msb){\n\/\/             cArr[index] = '1';\n\/\/         }\n\/\/         else {\n\/\/             cArr[index] = '0';\n\/\/         }\n\/\/         index++;\n\/\/         bitNo--;\n\/\/     }\n\/\/     cArr[index] = '\\0';  \n\/\/ }<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>And here is the output:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/floats_integers_to_char_arrays_and_strings-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"334\" height=\"239\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/floats_integers_to_char_arrays_and_strings-1.png\" alt=\"Output floats_integers_to_char_arrays_and_strings.ino\" class=\"wp-image-20149\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/floats_integers_to_char_arrays_and_strings-1.png 334w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/floats_integers_to_char_arrays_and_strings-1-300x215.png 300w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><\/a><figcaption class=\"wp-element-caption\">Output floats_integers_to_char_arrays_and_strings.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"char_arrays_and_strings_to_numbers\">Converting Character Arrays and String Objects into Numbers<\/h2>\n\n<p>The functions <code>atof()<\/code>, <code>atoi()<\/code> and <code>atol()<\/code> generate floating-point numbers, integers or long integers from character arrays:<\/p>\n<p><code>double myDouble = atof ( char* charArray ); \/\/ Alternative: float<\/code><\/p>\n<p><code>int myInt = atoi( char* charArray );<\/code><\/p>\n<p><code>long myLong = atol( char* charArray );<\/code><\/p>\n<p>The following options are available for generating numbers from String objects:<\/p>\n<p><code>float myFloat = myString.toFloat();<\/code><\/p>\n<p><code>int myInt = myString.toInt();  \/\/ Alternative: long myLong = myString.toInt();<\/code><\/p>\n<p>A few examples:<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"char_arrays_and_strings_to_integers_and_floats.ino\" data-enlighter-title=\"char_arrays_and_strings_to_integers_and_floats.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray_1[] = \"123.4567\";\n    char myCharArray_2[] = \"4242\";\n   \n    float myFloatfromCharArray = atof(myCharArray_1);\n    Serial.print(\"Float from char array: \");\n    Serial.println(myFloatfromCharArray, 4);\n\n    int myIntfromCharArray = atoi(myCharArray_2);\n    Serial.print(\"Integer from char array: \");\n    Serial.println(myIntfromCharArray);\n    Serial.println();\n\n    String myString_1 = \"123.4567\";\n    String myString_2 = \"4242\";\n    String myString_3 = \"42.42blabla\";\n\n    float myFloatfromString = myString_1.toFloat(); \/\/ alternative: toDouble()\n    Serial.print(\"Float from String: \");\n    Serial.println(myFloatfromString);\n\n    int myIntFromString = myString_2.toInt();\n    Serial.print(\"Integer from String: \");\n    Serial.println(myIntFromString);\n\n    float myFloatfromString_2 = myString_3.toFloat();\n    Serial.print(\"Float from String, 2: \");\n    Serial.println(myFloatfromString_2);    \n}\n\nvoid loop(){}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>And here is the output:<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_arrays_and_strings_to_integers_and_floats.png\"><img loading=\"lazy\" decoding=\"async\" width=\"346\" height=\"110\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_arrays_and_strings_to_integers_and_floats.png\" alt=\"Output char_arrays_and_strings_to_integers_and_floats.ino\" class=\"wp-image-19942\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_arrays_and_strings_to_integers_and_floats.png 346w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_arrays_and_strings_to_integers_and_floats-300x95.png 300w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><\/a><figcaption class=\"wp-element-caption\">Output char_arrays_and_strings_to_integers_and_floats.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"reading_via_serial\">Reading Character Arrays and String Objects from Serial<\/h2>\n\n<p>If you receive data via the serial interface, you can convert it into character arrays or String objects. You have the choice of reading in all transmitted characters or only doing so up to a certain character.<\/p>\n<p>Let&#8217;s start with the character arrays:<\/p>\n<p><code>size_t length = Serial.readBytesUntil( char terminator, char* charArray, int maxLength );<\/code><\/p>\n<p><code>size_t length = Serial.readBytes( char* myCharArray, int maxLength );<\/code><\/p>\n<p>You have the following options for String objects:<\/p>\n<p><code>String myString = Serial.readStringUntil( char terminator );<\/code><\/p>\n<p><code>String myString = Serial.readString();<\/code><\/p>\n\n<p>Example sketch:<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"read_char_arrays_and_strings_from_serial.ino\" data-enlighter-title=\"read_char_arrays_from_serial.ino\">void setup(){\n    Serial.begin(9600);\n}\n\nvoid loop(){\n    char myCharArray[20] = {'\\0'};\n\n    if(Serial.available()){\n        Serial.readBytesUntil('\\n', myCharArray, sizeof(myCharArray));\n        Serial.println(myCharArray);\n    }\n}\n\n\/* alternative loop() *\/\n\/\/ void loop(){\n\/\/     char myCharArray[11] = {'\\0'}; \n\n\/\/     if(Serial.available()){\n\/\/         size_t length = Serial.readBytes(myCharArray, 10);\n\/\/         Serial.println(myCharArray);\n\/\/         Serial.println(length);\n\/\/     }\n\/\/ }<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"read_char_arrays_and_strings_from_serial.ino\" data-enlighter-title=\"read_strings_from_serial.ino\">void setup(){\n    Serial.begin(9600);\n}\n\nvoid loop(){\n    if(Serial.available()){\n        String myString = Serial.readStringUntil('\\n');\n        Serial.println(myString);\n    }\n}\n\n\/* Alternative that also captures \\n and \\r *\/\n\/\/ void loop(){\n\/\/     if(Serial.available()){\n\/\/         String myString = Serial.readString();\n\/\/         Serial.println(myString);\n\/\/     }\n\/\/ }<\/pre>\n<p>\n\n<p>Upload the example sketch(es) and send messages via the serial monitor. You could vary the length of the messages and see what you get as output.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"passing_to_functions\">Passing Character Arrays and String Objects to Functions<\/h2>\n\n<p>If you want to use a character array in a function, then you pass the name of the character array to the function. The name of the array represents the pointer to element zero. That means you use the original character array in the function. To use a local copy, you would first have to create it.<\/p>\n<p>If you need the size of the character array in the function, you must pass it separately. The length, on the other hand, is not a problem as it can be determined using the NULL terminator.<\/p>\n<p>If you want to use the original String object in a function, you must prefix the parameter in the receiving function with the reference operator &#8220;&amp;&#8221;.<\/p>\n<p>Let&#8217;s take a look at this in the following example sketch.<\/p>\n<\/p>\n<div class=\"scroll-paragraph\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"passing_char_arrays_and_strings_to_functions.ino\" data-enlighter-title=\"passing_char_arrays_and_strings_to_functions.ino\">void setup(){\n    Serial.begin(9600);\n    char myCharArray[20] = \"I am a char array\";\n    String myString = \"I am a string\";\n\n    Serial.println(myCharArray);\n    modifyCharArray(myCharArray, sizeof(myCharArray));\n    Serial.println(myCharArray);\n    Serial.println();\n\n    Serial.println(myString);\n    modifyString(myString);\n    Serial.println(myString);\n}\n\n\/* alternative loop() *\/\nvoid loop(){}\n\nvoid modifyCharArray(char* cArr, size_t size){\n    Serial.print(\"Size of cArr: \");\n    Serial.println(sizeof(cArr));\n    Serial.print(\"Size of myCharArray: \");\n    Serial.println(size);\n    cArr[strlen(cArr)] = '!';\n    cArr[strlen(cArr) + 1] = '\\0';\n    Serial.println(cArr);\n}\n\nvoid modifyString(String &amp;str){\n    str += \"!\";\n    Serial.println(str);\n}<\/pre>\n<p>\u00a0<\/p>\n<\/div>\n<p>\n\n<p>As you can see from the output, we have changed the originals of the character array and the String object in the functions.<\/p>\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/passing_char_arrays_and_strings.png\"><img decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/passing_char_arrays_and_strings.png\" alt=\"Output passing_char_arrays_and_strings_to_functions.ino\" class=\"wp-image-20039\"\/><\/a><figcaption class=\"wp-element-caption\">Output passing_char_arrays_and_strings_to_functions.ino<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\">Acknowledgement<\/h2>\n\n<p>The background of my post picture comes from <a href=\"https:\/\/pixabay.com\/de\/users\/lenaertsdaan-535541\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1128227\" target=\"_blank\" rel=\"noopener\">Daan Lenaerts<\/a> on <a href=\"https:\/\/pixabay.com\/de\/\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1128227\" target=\"_blank\" rel=\"noopener\">Pixabay<\/a>. The foreground I modified comes from <a href=\"https:\/\/pixabay.com\/de\/users\/openclipart-vectors-30363\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=148970\" target=\"_blank\" rel=\"noopener\">OpenClipart-Vectors<\/a>, also on <a href=\"https:\/\/pixabay.com\/de\/\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=148970\" target=\"_blank\" rel=\"noopener\">Pixabay<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Character arrays are less convenient than strings, but save resources. I show that character arrays are in no way inferior to strings in terms of functionality. <\/p>\n","protected":false},"author":1,"featured_media":19990,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[575],"tags":[556,2394,2393,2387,2362,2390,2385,2360,2371,2400,2382,2399,2383,2384,2389,2402,2406,2401,2388,2378,2403,2379,2365,2375,2396,2407,2398,2372,2395,2377,2380,2367,2361,2386,2392,2369,2366,2381,2376,2368,2373,2374,2364,2363,2370],"class_list":["post-20152","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-and-tools","tag-arduino-en-2","tag-atof-en","tag-atoi-en","tag-binary","tag-c_str-en","tag-char-array-to-float-en","tag-char-array-to-string-en","tag-char-arrays-en-2","tag-charat-en","tag-compare","tag-compareto-en","tag-duplicate","tag-equals-en","tag-equalsignorecase-en","tag-float-to-char-array-en","tag-float-to-character-array","tag-float-to-string-en-2","tag-float-to-string-en","tag-hexadecimal","tag-indexof-en","tag-integer-to-character-array","tag-lastindexof-en","tag-length-en","tag-memcpy-en","tag-readbytesuntil-en","tag-readstring-en","tag-readstringuntil-en","tag-remove-en","tag-serial-en","tag-strchr-en","tag-strcmp-en","tag-strdup-en","tag-string-en","tag-string-to-char-array-en","tag-string-to-float-en","tag-strlwr-en","tag-strncat-en","tag-strrchr-en","tag-strtok-en","tag-strupr-en","tag-substring-en","tag-substring-en-2","tag-tochararray-en","tag-toint-en","tag-tolowercase-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Character Arrays vs. Strings &#8226; Wolles Elektronikkiste<\/title>\n<meta name=\"description\" content=\"Character arrays are less convenient than strings, but save resources. I show that character arrays have the same functionality.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Character Arrays vs. Strings &#8226; Wolles Elektronikkiste\" \/>\n<meta property=\"og:description\" content=\"Character arrays are less convenient than strings, but save resources. I show that character arrays have the same functionality.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings\" \/>\n<meta property=\"og:site_name\" content=\"Wolles Elektronikkiste\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-01T21:18:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-10-30T20:08:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_v2.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"948\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Wolfgang Ewald\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Wolfgang Ewald\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"28 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings\"},\"author\":{\"name\":\"Wolfgang Ewald\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"headline\":\"Character Arrays vs. Strings\",\"datePublished\":\"2024-02-01T21:18:59+00:00\",\"dateModified\":\"2025-10-30T20:08:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings\"},\"wordCount\":3375,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/char_array_v2.webp\",\"keywords\":[\"Arduino\",\"atof()\",\"atoi()\",\"binary\",\"c_str()\",\"char array to float\",\"char array to string\",\"char arrays\",\"charAt()\",\"compare\",\"compareTo()\",\"duplicate\",\"equals()\",\"equalsIgnoreCase()\",\"float to char array\",\"float to character array\",\"Float to String\",\"float to string\",\"hexadecimal\",\"indexOf()\",\"integer to character array\",\"lastIndexOf()\",\"length()\",\"memcpy()\",\"readBytesUntil()\",\"readString()\",\"readStringUntil()\",\"remove()\",\"Serial\",\"strchr()\",\"strcmp()\",\"strdup()\",\"String\",\"string to char array\",\"string to float\",\"strlwr()\",\"strncat()\",\"strrchr()\",\"strtok()\",\"strupr()\",\"substring\",\"substring()\",\"toCharArray()\",\"toInt()\",\"toLowerCase()\"],\"articleSection\":[\"Software and tools\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings\",\"name\":\"Character Arrays vs. Strings &#8226; Wolles Elektronikkiste\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/char_array_v2.webp\",\"datePublished\":\"2024-02-01T21:18:59+00:00\",\"dateModified\":\"2025-10-30T20:08:16+00:00\",\"description\":\"Character arrays are less convenient than strings, but save resources. I show that character arrays have the same functionality.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#primaryimage\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/char_array_v2.webp\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/char_array_v2.webp\",\"width\":1200,\"height\":948},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/character-arrays-vs-strings#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Character Arrays vs. Strings\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\",\"name\":\"Wolles Elektronikkiste\",\"description\":\"Die wunderbare Welt der Elektronik\",\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\",\"name\":\"Wolfgang Ewald\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"width\":512,\"height\":512,\"caption\":\"Wolfgang Ewald\"},\"logo\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Character Arrays vs. Strings &#8226; Wolles Elektronikkiste","description":"Character arrays are less convenient than strings, but save resources. I show that character arrays have the same functionality.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings","og_locale":"en_US","og_type":"article","og_title":"Character Arrays vs. Strings &#8226; Wolles Elektronikkiste","og_description":"Character arrays are less convenient than strings, but save resources. I show that character arrays have the same functionality.","og_url":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings","og_site_name":"Wolles Elektronikkiste","article_published_time":"2024-02-01T21:18:59+00:00","article_modified_time":"2025-10-30T20:08:16+00:00","og_image":[{"width":1200,"height":948,"url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_v2.webp","type":"image\/webp"}],"author":"Wolfgang Ewald","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Wolfgang Ewald","Est. reading time":"28 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#article","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings"},"author":{"name":"Wolfgang Ewald","@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"headline":"Character Arrays vs. Strings","datePublished":"2024-02-01T21:18:59+00:00","dateModified":"2025-10-30T20:08:16+00:00","mainEntityOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings"},"wordCount":3375,"commentCount":4,"publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_v2.webp","keywords":["Arduino","atof()","atoi()","binary","c_str()","char array to float","char array to string","char arrays","charAt()","compare","compareTo()","duplicate","equals()","equalsIgnoreCase()","float to char array","float to character array","Float to String","float to string","hexadecimal","indexOf()","integer to character array","lastIndexOf()","length()","memcpy()","readBytesUntil()","readString()","readStringUntil()","remove()","Serial","strchr()","strcmp()","strdup()","String","string to char array","string to float","strlwr()","strncat()","strrchr()","strtok()","strupr()","substring","substring()","toCharArray()","toInt()","toLowerCase()"],"articleSection":["Software and tools"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings","url":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings","name":"Character Arrays vs. Strings &#8226; Wolles Elektronikkiste","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#website"},"primaryImageOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#primaryimage"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_v2.webp","datePublished":"2024-02-01T21:18:59+00:00","dateModified":"2025-10-30T20:08:16+00:00","description":"Character arrays are less convenient than strings, but save resources. I show that character arrays have the same functionality.","breadcrumb":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#primaryimage","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_v2.webp","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2024\/01\/char_array_v2.webp","width":1200,"height":948},{"@type":"BreadcrumbList","@id":"https:\/\/wolles-elektronikkiste.de\/en\/character-arrays-vs-strings#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/wolles-elektronikkiste.de\/en"},{"@type":"ListItem","position":2,"name":"Character Arrays vs. Strings"}]},{"@type":"WebSite","@id":"https:\/\/wolles-elektronikkiste.de\/en#website","url":"https:\/\/wolles-elektronikkiste.de\/en","name":"Wolles Elektronikkiste","description":"Die wunderbare Welt der Elektronik","publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wolles-elektronikkiste.de\/en?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46","name":"Wolfgang Ewald","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","width":512,"height":512,"caption":"Wolfgang Ewald"},"logo":{"@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png"}}]}},"_links":{"self":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/20152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/comments?post=20152"}],"version-history":[{"count":2,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/20152\/revisions"}],"predecessor-version":[{"id":25062,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/20152\/revisions\/25062"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media\/19990"}],"wp:attachment":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media?parent=20152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/categories?post=20152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/tags?post=20152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}