Node.js StringDecoder
Node.js StringDecoder
When we say about the Node.js StringDecoder, this is mainly used to decode buffer into the string. This is similar to buffer.toString() but this will provides extra support to UTF.
And to use StringDecoder module you need to use require('string_decoder'). And below is the syntax for this:-
const StringDecoder = require('string_decoder').StringDecoder;
Node.js StringDecoder Methods
StringDecoder class has two methods and these are given below:-
(1)decoder.write(buffer):-Well this method is mainly used to return the decoded string.
(2)decoder.end():-Well this method is used to return trailing bytes, if any left in the buffer.
Node.js StringDecoder Example
Well to understand the StringDecoder we will take a example of StringDecoder:-
File: stringDecoderExample.js
Now the output of above code for StringDecoder is given below:-
| |