Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

nodejs: string manipulation

nodejs: string manipulation

Problem

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

But substr doesn't appear to work!

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

Many thanks in advance,

Problem courtesy of: Eamorr

Solution

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

Solution courtesy of: fe_lix_

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

nodejs: string manipulation

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×