[last updated: 2022-08-02]
go to: Arduino programming
-----
On this page:
- Type String:
- define a String
- get length of a String
- concatenate a String
- convert String to char array
- remove a portion of a String
- get a portion of a String
- convert some other dataType to String
- Type char:
- Type char array:
- define a char array
- reference an individual element of an array
- get length of an array
- convert array to String
==============================================
- 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 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)
- ...
-----------------------------------------------------
- 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);
- 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