The lastIndexOf()
method checks for the search term in the buffer object and returns it.
If the search term occurs more than once in a buffer, it will return the position of the last occurrence.
buffer.lastIndexOf(value, start, encoding);
value
: This is the search term that needs to be searched in the buffer.start
: This signals where to begin the search in the buffer. The default is 0
.encoding
: If the search term is string
, then this parameter is used to specify the encoding.The lastIndexOf()
method returns the last occurrence for given search term.
This method returns
-1
if the search term is not found in the buffer.
In the example below:
i
in the buf
. Here, we have multiple occurrences of i
: one is at index 2
, and another is at index 5
. So, the lastIndexOf
method will return the last occurrence, that is index 5
.const buf = Buffer.from("This is an example") //getting last index of i console.log(buf.lastIndexOf('i'))
In the example below:
z
, which is not present in the buffer object buf
, so the lastIndexOf
method will return -1
.const buf = Buffer.from("This is an example") //accessing the index of z which is not present in buffer console.log(buf.lastIndexOf('z'))
RELATED TAGS
CONTRIBUTOR
View all Courses