I have the following node.js code:
conn.on("data",function(x){
var responseData=x;
//sys.puts(responseData);
sys.puts(responseData.length);
var f=50;
var N=responseData.length;
if(N>f){
var p=Math.floor(N/f);
var p_rem=N%f;
var hash="";
for(var i=0;i<p;i++){
hash=DJBHash(responseData.substr(f*i,f)); //this line causes program to exit!
sys.puts(responseData.substr(f*i,f)+"***"+hash);
}
}
soc.write(x);
});
But substr doesn't appear to work!
How can I get substrings of a string in node.js?
Many thanks in advance,
In JavaScript, strings are immutable and help us to store text that includes characters, numbers, and Unicode. Also, JavaScript includes many built-in functions for creating and manipulating strings in various ways.
Strings in JavaScript are immutable. This means that you cannot modify an existing string, you can only create a new string.
String literals can be specified using single or double quotes, which are treated identically, or using the backtick character ` .
The variable data is of type Buffer, you would have to create a string with the method toString and then, you will be able to do the substr. Something like that will work :
responseData.toString().substr(1)
For more info consult this link :
http://nodejs.org/docs/v0.4.10/api/buffers.html#buffer.toString
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With