Javatpoint Logo
Javatpoint Logo

Arduino String Object

What are objects in Arduino?

An object is like a variable, which points to a memory location that holds some data. The functions associated with the object are called member functions. As a result, we can make the objects to perform some actions.

For example,

The begin(), print(), and println() are the functions that are declared using the serial object. The period after the Serial (print(), etc.) specifies that we are accessing some members within the serial object. The members can be either a function or a variable.

The parentheses after the print() and println() function determines the function being called in the serial. These functions cannot be declared alone in the global scope in the code. Thus, they are declared with the serial object as Serial.print() and Serial.println().

String Object

The String object allows us to store and perform actions on an array of characters. The String object takes more memory than the regular String.

The String object is always displayed with the uppercase 'S'. It produces an instance of the String class.

It can be constructed from different data types, which are listed below:

  • a constant integer using a specific base
  • a long integer using a particular base
  • an instance of a String object
  • a constant character enclosed within a single-quotes
  • A constant string of characters enclosed within the double-quotes.
  • float and double

The specified number in a string has a default base of 10. For example,

It creates a string that consists of the ASCII representation of the number 15.

To specify a number with base other than 10, the String can be created as:

It will give us the String ''F''. It signifies the hexadecimal conversion of the decimal number 15.

We can also represent a String in BIN. For example,

It will give us the String ''1111''. It signifies the hexadecimal conversion of the decimal number 15.

The syntax is:

where,

value: It signifies the value needed to format or represent a string.

The data types are byte, unsigned int, char, float, double, unsigned long, long, int, and string.

base: It signifies the base, which is required to format the integer value. For example, HEX, BIN, etc.

The base is optional to include.

PlacesofaDecimal: It specifies the desired decimal places, which is only needed if the value is double or float.

Functions with String Object

We can use various functions with the String.

  • charAT()

It is used to access a specific character.

The syntax is:

where,

thisString is the declared variable

n is also a variable

The data type allowed is unsigned int.

The above syntax will return the nth character of the specified string.

  • compareTo()

It is used to compare the two Strings. The specified Strings are compared using the ASCII values of the characters.

The syntax is:

where,

thisString and myString are the declared variable

The above syntax will return a negative number if thisString comes before myString.

It will return a positive number if thisString comes after myString.

  • endsWith()

It tests that if a String ends with the characters of other String or not.

The syntax is:

where,

thisString and myString are the declared variable

It will return true if thisString ends with the characters of myString. Otherwise, it will return false.

  • startsWith()

It tests that if a String starts with the characters of other String or not.

The syntax is:

where,

thisString and myString are the declared variable

It will return true if thisString starts with the characters of myString. Otherwise, it will return false.

  • toLowerCase()

It gets a lowercase version of the specified String.

The syntax is:

where,

thisString is the declared variable

  • toUpperCase()

It gets an uppercase version of the specified String.

The syntax is:

where,

thisString is the declared variable

  • equals()

It compares the equality of the Two Strings. The comparison is quite sensitive, where String "Arduino" is not equal to the String "ARDUINO".

The syntax is:

where,

thisString and myString are the variables

It will return true if thisString equals to myString. Otherwise, it will return false.

  • getBytes()

It copies the characters of the String to the supplied buffer.

The syntax is:

where,

thisString is the declared variable

buffer: The characters are copied into the buffer. The required data type is the byte.

length: It signifies the size of the buffer. The required data type is unsigned int.

  • lastIndexOf()

It locates a String or character within the other String.

The syntax is:

where,

thisString is the declared variable

value: It includes the particular value to search for. The required data types are String and char.

from: It is defined as the index to work backward from

  • substring()

It is declared to get the substring of a specific String.

The syntax is:

where,

thisString is the declared variable

from: It is defined as the index, from where the substring will start

to: It is also the index to end the substring before. It is optional.

  • toInt()

It converts a valid String in Arduino to the integer type.

The syntax is:

where,

thisString is the variable

The required data type is long. It returns zero if the conversation is invalid or a String does not start with an integer number.

  • toFloat()

It converts a valid String in Arduino to the float.

For example, String "150" will be converted to "150.00"

The syntax is:

where,

thisString is the variable

The required data type is float. It returns zero if the conversation is invalid or a String does not start with a digit.

  • trim()

It modifies the String by removing the extra whitespaces.

The syntax is:

where,

thisString is the variable

Code Example

Let's understand the String Object with two examples.

Example 1:

Let's use the charAt() function.

Consider the below code:

Output: t

The t is the character at index 8, which is declared in the charAt() function.

Example 2:

Let's use the concat function.

concat() - The concat() function is used to combine the Strings.

Consider the below code:

Output

Arduino String Object

Similarly, we can use other String object functions as well.


Next TopicBlinking an LED




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA