diff --git a/doc/api/buffer.markdown b/doc/api/buffer.markdown index ab1ee5a6c075ee..5b004e1bca589f 100644 --- a/doc/api/buffer.markdown +++ b/doc/api/buffer.markdown @@ -593,6 +593,46 @@ for (var key of buf.keys()) { // 5 ``` +### buf.lastIndexOf(value[, byteOffset][, encoding]) + +* `value` {String|Buffer|Number} +* `byteOffset` {Number} Default: `buf.length` +* `encoding` {String} Default: `'utf8'` +* Return: {Number} + +Identical to [`Buffer#indexOf()`][], but searches the Buffer from back to front +instead of front to back. Returns the starting index position of `value` in +Buffer or `-1` if the Buffer does not contain `value`. The `value` can be a +String, Buffer or Number. Strings are by default interpreted as UTF8. If +`byteOffset` is provided, will return the last match that begins at or before +`byteOffset`. + +```js +const buf = new Buffer('this buffer is a buffer'); + +buf.lastIndexOf('this'); + // returns 0 +buf.lastIndexOf('buffer'); + // returns 17 +buf.lastIndexOf(new Buffer('buffer')); + // returns 17 +buf.lastIndexOf(97); // ascii for 'a' + // returns 15 +buf.lastIndexOf(new Buffer('yolo')); + // returns -1 +buf.lastIndexOf('buffer', 5) + // returns 5 +buf.lastIndexOf('buffer', 4) + // returns -1 + +const utf16Buffer = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); + +utf16Buffer.lastIndexOf('\u03a3', null, 'ucs2'); + // returns 6 +utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2'); + // returns 4 +``` + ### buf.length * {Number}