Arduino - String() Objects - MyHomeThings

Skip to content

Creates an instance of the String class. The String class can also create strings from different data types. An instance of a String class can be created as easily as a variable and assigned a value.

String myString = "This is a String object.";

You can also create a String object from a number. A string according to the ASCII table is obtained. The 20 numbers will become a “20” string.

String myString = (20);

The following example is the decimal 20, HEX equivalent 14.

String myString = (20, HEX);

The 20 binary form of the decimal will be 00010100.

String myString = (20, BIN);

The result of the example below will be 20.69, in the second parameter you can specify how many decimal places to display and do this by rounding. It only works with float and double data types.

String thisString = String(20.68859, 2);

Strings can also be concatenated.

void setup() { Serial.begin(9600); String myString1 = "Hello "; String myString2 = "World"; String myString3 = myString1 + myString2; Serial.println(myString3); } void loop() { }

We can also compare Strings. The comparison is case sensitive, meaning “Hello” is not equal to “HELLO” String.

void setup() { Serial.begin(9600); String myString1 = "Hello"; String myString2 = "HELLO"; if(myString1 == myString2) { Serial.println("true"); } else { Serial.println("false"); } } void loop() { }

You can manipulate strings with the following functions:

charAt () Returns the nth character of the String. Here, too, indexing begins with zero.

void setup() { Serial.begin(9600); String myString = "Hello"; unsigned int n = 1; Serial.println(myString.charAt(n)); } void loop() { }

compareTo () Compares two strings, testing whether one is in front of or behind the other, or equal to. It compares strings on a character-by-character basis with the ASCII values ​​of the characters and returns the number of differences between them. This number can also be a negative number.

void setup() { Serial.begin(9600); String myString1 = "hello"; String myString2 = "World"; int n; n = myString1.compareTo(myString2); Serial.println(n); } void loop() { }

concat () appends the parameter to a string.

String myString = "Hello"; myString.concat(" World");

endsWith () Checks if a String ends with another String character. If yes returns true, otherwise false.

void setup() { Serial.begin(9600); String myString1 = "Hello"; String myString2 = "o"; if(myString1.endsWith(myString2)) { Serial.println("true"); } else { Serial.println("false"); } } void loop() { }

equals () Same as the comparison operator in the previous example “==” The comparison is case sensitive, meaning “Hello” is not equal to the “HELLO” String.

void setup() { Serial.begin(9600); String myString1 = "Hello"; String myString2 = "HELLO"; if(myString1.equals(myString2)) { Serial.println("true"); } else { Serial.println("false"); } } void loop() { }

equalsIgnoreCase () Compares the two strings of equality. The comparison is not case sensitive, i.e. (“hello”) is equal to (“HELLO”) String.

void setup() { Serial.begin(9600); String myString1 = "Hello"; String myString2 = "HELLO"; if(myString1.equalsIgnoreCase(myString2)) { Serial.println("true"); } else { Serial.println("false"); } } void loop() { }

length () Returns the number of characters in the String without the trailing zero character.

String myString = "Hello World"; int length = myString.length(); Serial.println(length);

getBytes () Copies the characters of the String to the “buff” buffer of “len” length specified in the parameter.

String myString = "Hello World"; unsigned int len = myString.length() + 1; byte buff[len]; myString.getBytes(buff, len); for (int i = 0; i < len; i++) { Serial.println(buff[i]); }

indexOf () Searches for a character or string within another string. By default, it searches from the beginning of the String, but you can also start searching from a specific index by specifying the index in the second parameter, allowing you to find all occurrences of the character or String.

myString.indexOf(val); myString.indexOf(val, from); String myString = "Hello World, Hello Arduino"; int index = myString.indexOf("Hello"); Serial.println(index);

lastIndexOf () Searches for a character or string within another string. By default, it searches from the end of the String, but you can also start searching from a specific index by specifying the index in the second parameter, allowing you to find all occurrences of the character or String.

myString.lastIndexOf(val); myString.lastIndexOf(val, from); String myString = "Hello World, Hello Arduino"; int index = myString.lastIndexOf("Hello"); Serial.println(index);

remove () Removes characters from the String from the specified index to the end of the String, or from the specified index to the number of indexes specified in the count.

myString.remove(index); myString.remove(index, count); String myString = "Hello World"; myString.remove(2, 2); Serial.println(myString);

replace () Replaces all occurrences of substring1 in myString with substring2.

String substring1 = "Arduino"; String substring2 = "World"; String myString = "Hello Arduino!"; Serial.println(myString); myString.replace(substring1, substring2); Serial.println(myString);

setCharAt () Sets a character in the String at the “index” position with the “char” character. It has no effect on indexes outside the existing length of the String.

myString.setCharAt(index, char) String myString = "hello world"; Serial.println(myString); myString.setCharAt(0, 'H'); myString.setCharAt(6, 'W'); Serial.println(myString);

startsWith () Checks if a String starts with another String.

String myString = "Hello World"; String myString2 = "Hello"; if (myString.startsWith(myString2)) { Serial.println("Ez a String Hello-val kezdődik"); } else { Serial.println("Ez a String nem Hello-val kezdődik"); }

substring () Copies a substring from the String from the starting index. You can also specify an optional end index, takes to the end index copy the substring. If the terminating index is omitted, the substring copie lasts until the end of the String.

String myString = "Hello World"; String mySubString = myString.substring(0, 4); Serial.println(myString); Serial.println(mySubString);

toCharArray () Copy the String characters to the “buff” buffer.

String myString = "Hello World"; byte buff[myString.length() + 1]; myString.toCharArray(buff, myString.length() + 1);

toDouble () Converts a String to a double. The String must start with a digit.

String myString = "41.83"; double myDouble = myString.toDouble();

toInt () Converts a valid String to an integer. String must start with an integer.

String myString = "36"; int myInt = myString.toInt();

toFloat () Converts a valid String to a float. The String must start with a digit.

String myString = "41.83"; float myFloat = myString.toFloat();

toLowerCase () Converts the String to lowercase.

myString = "HELLO WORLD"; Serial.println(myString); myString.toLowerCase(); Serial.println(myString);

toUpperCase () Converts a String to uppercase.

myString = "hello world"; Serial.println(myString); myString.toUpperCase(); Serial.println(myString);

trim () Removes spaces from the beginning and end of a String.

myString = " Hello World "; Serial.println(myString.length()); myString.trim(); Serial.println(myString.length());
amazon logo
Official Arduino Starter Kit with English Projects Book

Official Arduino Starter Kit with English Projects Book

advertising – amazon.com

amazon logo
ELEGOO UNO ATmega328P R3 Board with USB Cable

ELEGOO UNO ATmega328P R3 Board with USB Cable

amazon logo
2 Channel 5V Relay Module with Optocoupler Low Level Trigger Expansion Board

2 Channel 5V Relay Module with Optocoupler Low Level Trigger Expansion Board

So much for the Strings. In the next section, we will review Arduino serial communication.

strings

Arduino For Beginners

Arduino serial communication

amazon
ESP-32S WiFi Development Board NodeMCU ESP-WROOM-32 Microcontroller 2.4GHz Dual-Core WiFi Bluetooth

ESP-32S NodeMCU WiFi Development Board

amazon
ESP8266 NodeMCU CP2102 ESP-12E Development Board

ESP8266 NodeMCU CP2102 ESP-12E Development Board

amazon
ESP8266 Serial Wifi Module ESP-01

ESP8266 Serial Wifi Module ESP-01

amazon
ESP01 ESP8266 Programmer Serial Adapter, USB to ESP-01 Wireless Programming Debug WiFi Module with CH340G, 4.5-5.5V UART PROG

ESP01 ESP8266 Programmer Serial Adapter, USB to ESP-01

advertising – ESP boards from amazon.com

Search for: Manage consent We use cookies on our websites to provide you with the most relevant experience. By clicking on the "Accept cookies" button, you consent to the use of ALL cookies.. Required Cookies Required Cookies Always active Cookies are absolutely necessary for the operation of the website Preferences Preferences The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user. Statistical Statistical The technical storage or access that is used exclusively for statistical purposes. Cookies used for anonymous statistical purposes. Information stored or retrieved for this purpose will not normally be used to identify you. Marketing Cookies Marketing Cookies Marketing Cookies allow it, to track users on one or more sites for advertising and other marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Accept cookies Required Cookies Settings Save settings Settings
  • {title}
  • {title}
  • {title}
Manage consent

Shelly Pro 4PM Wi-Fi LAN, 4-Channel Smart Relay with Power Metering

Shelly Pro 4PM | Wi-Fi & LAN, 4-Channel Smart Relay with Power Metering

Bluetooth Wireless Speakers, 80W Peak

Bluetooth Wireless Speakers, 80W Peak

Honeywell Wi-Fi Color Touch Screen Programmable Thermostat

Honeywell Wi-Fi Color Touch Screen Programmable Thermostat

Rollo USB Shipping Label Printer

Rollo USB Shipping Label Printer

ELEGOO UNO Project Super Starter Kitwith Tutorial and UNO R3 Board Compatible with Arduino IDE

ELEGOO UNO Project Super Starter Kit with Tutorial and UNO R3 Board Compatible with Arduino IDE

The advertisements appearing on this site help to maintain it.

Shelly Pro 4PM Wi-Fi LAN, 4-Channel Smart Relay with Power Metering

Shelly Pro 4PM | Wi-Fi & LAN, 4-Channel Smart Relay with Power Metering

Bluetooth Wireless Speakers, 80W Peak

Bluetooth Wireless Speakers, 80W Peak

Honeywell Wi-Fi Color Touch Screen Programmable Thermostat

Honeywell Wi-Fi Color Touch Screen Programmable Thermostat

Rollo USB Shipping Label Printer

Rollo USB Shipping Label Printer

ELEGOO UNO Project Super Starter Kitwith Tutorial and UNO R3 Board Compatible with Arduino IDE

ELEGOO UNO Project Super Starter Kit with Tutorial and UNO R3 Board Compatible with Arduino IDE

The advertisements appearing on this site help to maintain it.

Tag » Arduino Remove N