Main Menu

search

You are here

Arduino: Text/Character programming:

[last updated: 2025-05-26]
Arduino programming
-----

On this page:

  • Type String:
    • define a String
    • get length of a String
    • concatenate a String
    • convert String to char array
    • convert String to integer
    • remove a portion of a String
    • get a portion of a String
    • convert some other dataType to String
  • Type char:
    • convert char to int
  • Type char array:
    • define a char array
    • reference an individual element of an array
    • get length of an array
    • convert array to String
    • Compare two arrays (strcmp)

==============================================

  • Type String:
    • Define a String:
      String str01 = "a";
      String str01 = "abc";
      String str02 = "def";
      String str01 = String(52); // using 'String Constructor' to define str01 as "52"
      String str0x = 'a'; // THIS DOES NOT WORK
    • Get length of a String:
      int str03_length = str03.length(); (note does not include appended null char)
    • Concatenate a String:
      [Always initialize strings before concatenating them]
      str01 = str01 + " plus this";
      str01 += " plus this";
      String str03 = str01 + str02;
      str01.concat(" plus this");
      str01.concat(str02);
      str03 = str01 + 'A'; // adding a constant character
      str03 = str01 + "abc"; // adding a constant string
      str01.concat(1234); // add an integer
      str03 = str01 + 123456789; // adding a constant long interger
      str03 = str01 + millis();
      str03 = str01 + analogRead(A0);
          these work because millis and analogRead return an integer or long int,
          which can be added to a String
    • Change a String to an integer:
      inString.toInt()
    • Change a String to a char array:
      [stringVarName].toCharArray([charArrayName], [charArrayLength])
      String str01 = "abc";
      char newCharArray[10];
      str01.toCharArray(newCharArray,10);
    • String str = "This is my string";
      // Length (with one extra character for the null terminator)
      int str_len = str.length() + 1;
      // Prepare the character array (the buffer)
      char char_array[str_len];
      // Copy it over
      str.toCharArray(char_array, str_len);
    • Remove a portion of a String:
      String str01 = "Hello World";
      str01.remove(8); // removes chars starting at index 8 (='r') to end of String,
         so that str01 is now: "Hello Wo"
      OR:
      str01.remove(2, 6); // removes 6 chars starting at index 2
         so that str01 is now: "Herld"
    • Get a portion of a String:
      String partOfString = wholeString.substring(from, to);
      where partOfString will include the char at the 'from' index, but not the char at the 'to' index.
      If the 'to' index is omitted, the substring will go to the end of the orig wholeString
        Get a single-char (as type char) portion of a String:
        wholeString.charAt(position)
    • Convert some other dataType to String:
      given a non-String variable oldVar,
      first define your new String:
            String newString;
      then convert oldVar:
            newString = String(oldVar);

    -----------------------------------------------------

  • Type Char:
    • Convert char to int:
      Suppose you have a char = "3", and you want to convert it to the integer 3:
            char01 = "3";
            int int01 = char01 - '0';
      This works because the ASCII code for '3' is 51 and the ASCII for '0' is 48 so 51 - 48 = 3 (as int)
    • convert byte ascii_value = 65:
      char text_char = (char)ascii_value; will store the character 'A' in text_char
    • Convert int to char:
      itoa ...

    -----------------------------------------------------

  • Type Char Array:
    • Define a char array:
      char var01[10] = "hgdf";
      char var2[] = "uytcf";
      char var3[10];
    • Reference an individual element of a char array:
      char var01[5] = "adfg"; // defines the array
      var01[0] is the first element of the array ('a')
      char oneElement = var01[2]; // will return 'f'
      int anotherElement = var01[3]; // returns the ascii value of 'g'
    • Get length of an array:
      int myCharArrayLength = sizeof(var03); // gives # of bytes in the array
    • Convert char array to String:
      Suppose you have a char array: oldCharArray
      and you want to convert it to a String: newStringFromCharArray
          String newStringFromCharArray = String(oldCharArray);
            However I could not get this to work on my current IDE 2.3.6...
    • Compare two arrays ("strings"):
      char tmp1[] = "some text";
      char tmp2[] = "other text";
      int cmp = strcmp(tmp1, tmp2);
      // if cmp = 0, then strings are equal
      // if cmp < 0, then str1 < str2
      // if cmp > 0, then str1 > str2
      In this example, cmp = 4, meaning tmp1 > tmp2
      Surely has to do with ascii values, but I'm otherwise clueless how to calculate/interpret this.
      If you're dealing with string representations of numbers, the situation is clear and easy to understand.

            However I could not get this to work on my current IDE 2.3.6.
      Cmp value changed each time through loop even though parameters didn't...

    • null termination notes:
      • char test1[] = {1,2,3,4,5};
        initializes the elements explicitly, so no terminating null is added. the compiler assumes you know what you're doing.
      • char test1[] = "chars";
        initializes the array with a string constant, ie by definition a string, so
        the compiler will add a null, and will dimension the array as 6 bytes long.
      • The null character is used by some methods to indicate the end of the string.
        eg. strcpy, strlen, etc. need some way to know when the end of the string is reached. the null provides that.
        If you never use such operations, and always access the array explicitly, eg. tmpAry[x], then you don't need the null termination.

  • Multi-dimensional char arrays:
    • char mainMenu[3][11] = { {"exit/run? "}, {"program? "}, {"manual? "} };
      then this will retrieve the nth char array:
      menuItemToDisplay = String(mainMenu[n]);

.

.

.

eof