nodejs:字符串操作 [英] nodejs: string manipulation

查看:137
本文介绍了nodejs:字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 node.js 代码:

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);
    });

但是 substr 似乎不起作用!

But substr doesn't appear to work!

如何在 node.js 中获取字符串的子字符串?

How can I get substrings of a string in node.js?

非常感谢,

推荐答案

变量数据是 Buffer 类型,你必须用 toString 方法创建一个字符串,然后你就可以做 substr.这样的事情会起作用:

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

这篇关于nodejs:字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆