Skip to content

Commit f02de13

Browse files
committed
Add docs for Buffer.lastIndexOf
1 parent c05a330 commit f02de13

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

doc/api/buffer.markdown

+40
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,46 @@ for (var key of buf.keys()) {
593593
// 5
594594
```
595595

596+
### buf.lastIndexOf(value[, byteOffset][, encoding])
597+
598+
* `value` {String|Buffer|Number}
599+
* `byteOffset` {Number} Default: `buf.length`
600+
* `encoding` {String} Default: `'utf8'`
601+
* Return: {Number}
602+
603+
Identical to [`Buffer#indexOf()`][], but searches the Buffer from back to front
604+
instead of front to back. Returns the starting index position of `value` in
605+
Buffer or `-1` if the Buffer does not contain `value`. The `value` can be a
606+
String, Buffer or Number. Strings are by default interpreted as UTF8. If
607+
`byteOffset` is provided, will return the last match that begins at or before
608+
`byteOffset`.
609+
610+
```js
611+
const buf = new Buffer('this buffer is a buffer');
612+
613+
buf.lastIndexOf('this');
614+
// returns 0
615+
buf.lastIndexOf('buffer');
616+
// returns 17
617+
buf.lastIndexOf(new Buffer('buffer'));
618+
// returns 17
619+
buf.lastIndexOf(97); // ascii for 'a'
620+
// returns 15
621+
buf.lastIndexOf(new Buffer('yolo'));
622+
// returns -1
623+
buf.lastIndexOf('buffer', 5)
624+
// returns 5
625+
buf.lastIndexOf('buffer', 4)
626+
// returns -1
627+
628+
const utf16Buffer = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
629+
630+
utf16Buffer.lastIndexOf('\u03a3', null, 'ucs2');
631+
// returns 6
632+
utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2');
633+
// returns 4
634+
```
635+
596636
### buf.length
597637

598638
* {Number}

0 commit comments

Comments
 (0)