Arduino – String() objects

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());

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

advertising – ESP boards from amazon.com