Arduino String Function

In this tutorial, we will cover all Arduino String Function like string replace function Arduino, string compare function in Arduino, string copy function Arduino, etc.

For datatype conversion like; int to string function arduino, string to int arduino, string to char arduino follow the link.

ARDUINO STRING FUNCTION

ARDUINO STRING FUNCTION

charAt()

myString.charAt(n): Access a particular character of the String.

where,

    • myString → String
    • n → Integer

Returns: character value at nth index

// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString = "HELLO";
char ch;
void setup() {
  Serial.begin(9600);
  delay(1000);

  ch = myString.charAt(2);
  Serial.print("myString: ");
  Serial.print(myString);
  Serial.print(", myString.charAt(2): ");
  Serial.println(ch);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

charAt arduino

compareTo()

myString.compareTo(myString2): Compares two Strings, testing whether one comes before or after the other, or whether they’re equal.

The strings are compared character by character, according to ASCII values of the characters.

Example: ‘A’, ‘B’, … ‘a’, ‘b’, … ‘a’ comes before ‘b’ but also ‘a’ comes after ‘A’ according to ASCII

You can also use operators like == and != to check if string equals to or not equals to

where,

    • myString → String
    • myString2 → String

Returns:

    • Negative (–)number if myString comes before myString2
    • Positive (+)number if myString comes after myString2
    • Zero (0) if String equals myString2
// Tutorial: https://pijaeducation.com/arduino-string-function/  

String myString = "HELLO";
String myString2 = "!HELLO";
String myString3 = "HELLO";
String myString4 = "HELLO!";
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("myString2 = ");
  Serial.println(myString2);
  Serial.print("myString3 = ");
  Serial.println(myString3);
  Serial.print("myString4 = ");
  Serial.println(myString4);
  Serial.println();
    
  n = myString.compareTo(myString2);
  Serial.print("myString.compareTo(myString2): ");
  Serial.print("n = ");
  Serial.println(n);

  n = myString.compareTo(myString3);
  Serial.print("myString.compareTo(myString3): ");
  Serial.print("n = ");
  Serial.println(n);

  n = myString.compareTo(myString4);
  Serial.print("myString.compareTo(myString4): ");
  Serial.print("n = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

string compareTo Arduino

concat()

myString.concat(parameter): Appends the parameter to a String.

where,

    • myString → String
    • parameter → can be String, string, char, byte, int, unsigned int, long, unsigned long, float, double, __FlashStringHelper(F() macro)

Returns: true (1) or false (0) (can be store in int or boolean)

// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString = "Apple: ";
int val = 5, n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString = ");
  Serial.println(myString);
  Serial.print("val = ");
  Serial.println(val);

  n = myString.concat(val);
  Serial.print("Returns = ");
  Serial.println(n);
  Serial.print("myString.concat(val) = ");
  Serial.println(myString);

  /* You can also add two string using + sign,
    / if you find some issue you can convert it into string to use + operator
    / String(val) or String(n) like this
  */
  myString = myString + val;
  Serial.print("myString after + = ");
  Serial.println(myString);

}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

string concat arduino

endsWith()

myString.endsWith(myString2): Checks if myString ends with the characters of myString2. This function is case sensitive.

where,

    • myString → String
    • myString2 → String

Returns: true (1) or false (0)

    • 1: if myString ends with the characters of myString2
    • 0: if myString doesn’t ends with the characters of myString2
// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString = "This is apple";
String myString2 = "apple";
String myString3 = "Apple";
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("myString2 = ");
  Serial.println(myString2);
  Serial.print("myString3 = ");
  Serial.println(myString3);

  Serial.println("### myString.endsWith(myString2)");
  n = myString.endsWith(myString2);
  Serial.print("Returns = ");
  Serial.println(n);

  Serial.println("### myString.endsWith(myString3)");
  n = myString.endsWith(myString3);
  Serial.print("Returns = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

string endsWith arduino

equals()

myString.equals(myString2): Compares two Strings for equality. The comparison is case-sensitive, meaning the String “apple” is not equal to the String “APPLE”
You can also use operators like == and != to check if string equals to or not equals to

where

    • myString → String
    • myString2 → String

Returns: true (1) or false (0)

    • 1: if myString equals (exact match) myString2
    • 0: if myString doesn’t match
// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString  = "apple";
String myString2 = "apple";
String myString3 = "Apple";
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("myString2 = ");
  Serial.println(myString2);
  Serial.print("myString3 = ");
  Serial.println(myString3);

  Serial.println("### myString.equals(myString2)");
  n = myString.equals(myString2);
  Serial.print("Returns = ");
  Serial.println(n);

  Serial.println("### myString.equals(myString3)");
  n = myString.equals(myString3);
  Serial.print("Returns = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

 

string equals arduino

equalsIgnoreCase()

myString.equalsIgnoreCase(myString2): Compares two Strings for equality. The comparison is not case-sensitive, meaning the String “apple” is equal to the String “APPLE”

where,

    • myString → String
    • myString2 → String

Returns: true (1) or false (0)

    • 1: if myString equals myString2 (not case sensitive)
    • 0: if myString doesn’t match
// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString  = "apple";
String myString2 = "apple";
String myString3 = "Apple";
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("myString2 = ");
  Serial.println(myString2);
  Serial.print("myString3 = ");
  Serial.println(myString3);

  Serial.println("### myString.equalsIgnoreCase(myString2)");
  n = myString.equalsIgnoreCase(myString2);
  Serial.print("Returns = ");
  Serial.println(n);

  Serial.println("### myString.equalsIgnoreCase(myString3)");
  n = myString.equalsIgnoreCase(myString3);
  Serial.print("Returns = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

string equalsIgnoreCase arduino

indexOf()

myString.indexOf(val): Locates a character or String within another String. By default, searches from the beginning of the String, but can also start from a given index, using myString.indexOf(val, from)

where

    • myString → String
    • val → char, String
    • from → Integer

Returns: The index of val within the String, or -1 if not found.

// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString  = "apple";
char val = 'e', val2 = 'E';
int from = 1;
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("val = ");
  Serial.println(val);
  Serial.print("val2 = ");
  Serial.println(val2);
  Serial.print("from = ");
  Serial.println(1);
  Serial.println();

  Serial.println("### myString.indexOf(val)");
  n = myString.indexOf(val);
  Serial.print("Return Index = ");
  Serial.println(n);

  Serial.println("### myString.indexOf(val, from)");
  n = myString.indexOf(val, from);
  Serial.print("Return Index = ");
  Serial.println(n);

  Serial.println("### myString.indexOf(val2)");
  n = myString.indexOf(val2);
  Serial.print("Return Index = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

indexOf string arduino

lastIndexOf()

myString.lastIndexOf(val): Locates a character or String within another String. By default, searches from the END of the String, but can work backwards from a given index using myString.lastIndexOf(val, from).

where

    • myString → String
    • val → char, String
    • from → Integer

Returns: The index of val within the String, or -1 if not found.

// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString  = "apple";
char val = 'p', val2 = 'A';
int from = 1;
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("val = ");
  Serial.println(val);
  Serial.print("val2 = ");
  Serial.println(val2);
  Serial.print("from = ");
  Serial.println(1);
  Serial.println();

  // This will return 2, as function we starts reading back from the last index
  Serial.println("### myString.lastIndexOf(val)");
  n = myString.lastIndexOf(val);
  Serial.print("Return Index = ");
  Serial.println(n);

  // This will return 1, as we starts reading back from index 1 'p' at index 2 is not read by function
  Serial.println("### myString.lastIndexOf(val, from)");
  n = myString.lastIndexOf(val, from);
  Serial.print("Return Index = ");
  Serial.println(n);

  Serial.println("### myString.lastIndexOf(val2)");
  n = myString.lastIndexOf(val2);
  Serial.print("Return Index = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

lastIndexOf string arduino

length()

myString.length(): Returns length of String (number of characters present)

where

    • myString → String

Returns: Integer

// Tutorial: https://pijaeducation.com/arduino-string-function/
/* myString.length(): Returns length of String (number of characters present)

   Returns: Integer

   myString  → String
*/
String myString   = "apple";
String myString2  = "apples are good for health";
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("myString2 = ");
  Serial.println(myString2);
  Serial.println();

  Serial.println("### myString.length()");
  n = myString.length();
  Serial.print("Length of String = ");
  Serial.println(n);

  Serial.println("### myString2.length()");
  n = myString2.length();
  Serial.print("Length of String 2 = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

string length arduino

remove()

myString.remove(index): Remove charaters of a String from index to end

myString.remove(index, n): Remove n number of charaters of a String from the index

where,

    • myString → String
    • index → integer
    • n → integer

Returns: Nothing

// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString   = "apples are good for health";
int index = 7, n = 4;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.println();

  Serial.println("### myString.remove(index, n)");
  myString.remove(index, n);
  Serial.print("New String = ");
  Serial.println(myString);
  Serial.println();

  Serial.println("### myString.remove(index)");
  myString.remove(index);
  Serial.print("New String = ");
  Serial.println(myString);

}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

string remove arduino

replace()

myString.replace(substring1, substring2): substring2 will replace substring1 in myString
where,

    • myString → String
    • substring1 → String
    • substring2 → String

Returns: Nothing

// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString   = "apples are good for health";
String substring1 = "are", substring2 = "***";

void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("substring1  = ");
  Serial.println(substring1);
  Serial.print("substring2  = ");
  Serial.println(substring2);
  Serial.println();

  Serial.println("### myString.replace(substring1, substring2)");
  myString.replace(substring1, substring2);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("substring1  = ");
  Serial.println(substring1);
  Serial.print("substring2  = ");
  Serial.println(substring2);
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

replace string replace

setCharAt()

myString.setCharAt(index, c): This will set or change character at particular index without changing string length

where,

    • myString → String
    • index → Integer
    • c → char

Returns: Nothing

 
// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString   = "Applet";
int index = 5;
char c = 's';
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("index = ");
  Serial.println(index);
  Serial.print("c = ");
  Serial.println(c);
  Serial.println();

  Serial.println("### myString.setCharAt(index, c)");
  myString.setCharAt(index, c);
  Serial.print("myString = ");
  Serial.println(myString);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

setCharAt string arduino

startsWith()

myString.startsWith(myString2): Checks if myString starts with the characters of myString2. This function is case sensitive.

where,

    • myString → String
    • myString2 → String

Returns: true (1) or false (0)

    • 1: if myString starts with the characters of myString2
    • 0: if myString doesn’t starts with the characters of myString2
// Tutorial: https://pijaeducation.com/arduino-string-function/

String myString = "This is apple";
String myString2 = "this";
String myString3 = "This";
int n;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("myString2 = ");
  Serial.println(myString2);
  Serial.print("myString3 = ");
  Serial.println(myString3);

  Serial.println("### myString.startsWith(myString2)");
  n = myString.startsWith(myString2);
  Serial.print("Returns = ");
  Serial.println(n);

  Serial.println("### myString.startsWith(myString3)");
  n = myString.startsWith(myString3);
  Serial.print("Returns = ");
  Serial.println(n);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

string startsWith

substring()

myString.substring(fromIndex): Returns substring, substring will be string from the ‘fromIndex’ to the ‘last’ of the myString

myString.substring(fromIndex, toIndex): substring will be string from the ‘fromIndex’ to ‘toIndex – 1’ of the myString

For example: if fromIndex = 1 and toIndex = 4, then substring length will be 4 – 1 = 3

where,

    • myString → String
    • fromIndex → Integer
    • toIndex → Integer

Returns: String

// TutoIndexrial: https://pijaeducation.com/arduino-string-function/

String myString = "This is an apple";
String myString2;
String myString3;
int fromIndex = 2, toIndex = 9;
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  =");
  Serial.println(myString);
  Serial.print("String Length = ");
  Serial.println(myString.length());
  Serial.print("fromIndex = ");
  Serial.println(fromIndex);
  Serial.print("toIndex = ");
  Serial.println(toIndex);
  Serial.println();
  
  Serial.println("### myString.substring(fromIndex)");
  myString2 = myString.substring(fromIndex);
  Serial.print("myString2 =");
  Serial.println(myString2);
  Serial.print("String Length = ");
  Serial.println(myString2.length());
  Serial.println();

  Serial.println("### myString.substring(fromIndex, toIndex)");
  myString3 = myString.substring(fromIndex, toIndex);
  Serial.print("myString3 =");
  Serial.println(myString3);
  Serial.print("String Length = ");
  Serial.println(myString3.length());


}

void loop() {
  // put your main code here, toIndex run repeatedly:

}

OUTPUT

substring arduino

toLowerCase()

myString.toLowerCase(): Will convert myString characters into lower case

where,

    • myString → String

Returns: Nothing

// TutoIndexrial: https://pijaeducation.com/arduino-string-function/

String myString = "This is an apple";
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.println();
  
  Serial.println("### myString.toLowerCase()");
  myString.toLowerCase();
  Serial.print("myString = ");
  Serial.println(myString);

}

void loop() {

}

OUTPUT

string toLowerCase arduino

toUpperCase()

myString.toUpperCase(): Will convert myString characters into upper case

where,

    • myString → String

Returns: Nothing

// TutoIndexrial: https://pijaeducation.com/arduino-string-function/

String myString = "This is an apple";
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.println();
  
  Serial.println("### myString.toUpperCase()");
  myString.toUpperCase();
  Serial.print("myString = ");
  Serial.println(myString);

}

void loop() {

}

OUTPUT

string toUpperCase arduino

trim()

myString.trim(): Will remove leading and trailing whitespaces from the string

where,

    • myString → String

Returns: Nothing

// TutoIndexrial: https://pijaeducation.com/arduino-string-function/

String myString = " This is an apple ";
void setup() {
  Serial.begin(9600);
  delay(200);

  Serial.print("myString  = ");
  Serial.println(myString);
  Serial.print("String Length = ");
  Serial.println(myString.length());
  Serial.println();
  
  Serial.println("### myString.trim()");
  myString.trim();
  Serial.print("myString = ");
  Serial.println(myString);
  Serial.print("String Length = ");
  Serial.println(myString.length());

}

void loop() {

}

OUTPUT

string trim arduino

 

4 4 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Dave
Dave
2 years ago

Thank you for taking the time to post this its just what I’d been looking for, many sites only tend to show the main functions with no examples, not easy trying to remember if Strings are 0 or 1 based between the various languages!

1
0
Would love your thoughts, please comment.x
()
x