- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the last occurrence of a substring within a string in Arduino
Just like indexOf() helps identify the first occurrence of a substring within a string, the lastIndexOf() function helps identify the last occurrence. This is because lastIndexOf() performs backward search, while indexOf() performs forward search.
syntax
myString.lastIndexOf(substr)
Where substr is the substring to search for in myString. It can be a character or a string.
Just like indexOf(), this function also accepts an optional from argument, in case you want the backward search to begin from a specific index. The syntax in that case is −
Syntax
myString.lastIndexOf(substr, from)
Just like indexOf(), this function either returns the last index of the substring within the string, or it returns -1 if no match is found.
Example
The following example code illustrates all of these points −
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); String s1 = "Mississippi"; String substr1 = "is"; String substr2 = "os"; Serial.println(s1.lastIndexOf(substr1)); Serial.println(s1.lastIndexOf(substr2)); Serial.println(s1.lastIndexOf(substr1, 3)); } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor Output is shown below −
As you can see, in the first case, the last index of the substring is returned (indexing starts with 0). In the second case, since no match was found, -1 was returned. In the third case, since we asked Arduino to start the backward scan from index 3, the next match, the index of the next match was returned.
- Related Articles
- Get the first occurrence of a substring within a string in Arduino
- Get the substring before the last occurrence of a separator in Java
- Find if a substring exists within a string in Arduino
- How to find index of last occurrence of a substring in a string in Python?
- Get the index of last substring in a given string in MySQL?
- Get the substring after the first occurrence of a separator in Java
- Finding the last occurrence of a character in a String in Java
- PHP – Find the last occurrence of a needle within a haystack using the iconv_strrpos() function
- How to get the last index of an occurrence of the specified value in a string in JavaScript?
- MySQL query to get a substring from a string except the last three characters?
- How to find the nth occurrence of substring in a string in Python?
- Get left part of the string on the basis of last occurrence of delimiter in MySQL?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- How to get everything before the last occurrence of a character in MySQL?
- Set characters at a specific position within the string in Arduino
