nodejs:字符串操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7081485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodejs: string manipulation
提问by Eamorr
I have the following node.js code:
我有以下 node.js 代码:
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!
但是 substr 似乎不起作用!
How can I get substrings of a string in node.js?
如何在 node.js 中获取字符串的子字符串?
Many thanks in advance,
提前谢谢了,
回答by fe_lix_
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 :
变量数据的类型为 Buffer,您必须使用 toString 方法创建一个字符串,然后才能执行 substr。这样的事情会起作用:
responseData.toString().substr(1)
For more info consult this link :
有关更多信息,请参阅此链接:
http://nodejs.org/docs/v0.4.10/api/buffers.html#buffer.toString
http://nodejs.org/docs/v0.4.10/api/buffers.html#buffer.toString

