• Node.js Video Tutorials

Node.js - Buffer.includes() Method



The NodeJS buffer.includes()method checks if the given value is available inside or included in the buffer. It returns true if present and false if not.

Syntax

Following is the syntax of the NodeJs includes() method −

buf.includes(value[, byteOffset][, encoding])

Parameters

The method buffer.includes() takes in three params.The first parameter is a mandatory one and the rest are all optional.

  • value −This is a mandatory parameter. It is the value to be searched inside the buffer.

  • byteoffset −This is optional parameter.It tells about the offset from where to start searching.If negative offset value is given it will search from the end of the buffer.The default value is 0.

  • encoding −If the value to be searched is a string then the encoding can be given using this parameter.It is an optional parameter.By default the encoding used is utf8.

Return value

The method buffer.includes() returns true if the value is found in the buffer , if not than false.

Example

In this example will try to search for a value inside the buffer created and test the result.

const buffer = Buffer.from('Welcome to TutorialsPoint');

if (buffer.includes('Welcome')) {
   console.log("The string welcome is present in the buffer");
} else {
   console.log("The string welcome is not present");
}

Output

We are searching for the string Welcome in the buffer created with the string "Welcome to Tutorialspoint". Since the given string is present we get below output.

The string welcome is present in the buffer

Example

Let us try by giving the byteOffset parameter in the example below −

const buffer = Buffer.from('Welcome to TutorialsPoint');
if (buffer.includes('Point', 10)) {
   console.log("The string point is present in the buffer");
} else {
   console.log("The string point is not present");
}

Output

We have used the byteOffset as 10. It will search in the string "Welcome to TutorialsPoint" from the offset of 10. Since the search string: Point is available you will get the output printed as "The string point is present in the buffer".

The string point is present in the buffer

Example

In this example will make use of a buffer as the value to check if it's available inside the given buffer.

const buffer = Buffer.from('Welcome to TutorialsPoint');
const result  = buffer.includes(Buffer.from('TutorialsPoint'));
if (result) {
   console.log("The string TutorialsPoint is present in the buffer");
} else {
   console.log("The string TutorialsPoint is not present");
}

Output

Inside the Buffer.includes() method we are making use of a buffer that has the string value: TutorialsPoint. Our main buffer is having a string: Welcome to TutorialsPoint. So since the string is present the value returned is true.

The string TutorialsPoint is present in the buffer

Example

Let us try a false case that is to check the response of Buffer.includes() when the string is not present.

const buffer = Buffer.from('Welcome to TutorialsPoint');

if (buffer.includes('Testing')) {
   console.log("The string Testing is present in the buffer");
} else {
   console.log("The string Testing is not present");
}

Output

We are searching for the string: Testing inside the buffer with string: Welcome to TutorialsPoint. Since it is not present it will return the value false. Hence you will get the output as: The string Testing is not present.

The string Testing is not present

Example

Let us test the encoding parameter inside Buffer.includes().

const buffer = Buffer.from('Hello World');
if (buffer.includes('Hello','hex')) {
   console.log("The string Hello is present in the buffer");
} else {
   console.log("The string Hello is not present");
}

Output

We have used "hex" encoding. When checked with encoding and the string is present it returns true.

The string Hello is present in the buffer

Example

In this example will make use of integer value i.e., the utf-8 encoding value to search in the buffer.

const buffer = Buffer.from('Hello World');
if (buffer.includes(72)) {
   console.log("The character H is present in the buffer");
} else {
   console.log("The string H is not present");
}

Output

Here 72 represents the character 'H' as per utf-8 encoding. When searched with the integer value in the buffer, it returns true as H character is present.

The string Hello is present in the buffer
nodejs_buffer_module.htm
Advertisements