string encoding

74 views
Skip to first unread message

Danil Gazizov

unread,
Aug 2, 2012, 9:15:35 AM8/2/12
to nod...@googlegroups.com
I'm confused of how to compare string const with variable.
Look at this simple example where keyboard input compares with const string.
I suggest this is encoding problem. Please, tell me what should be done to exit process ?
File encoding is utf8

process.stdin.setEncoding();//default is utf-8
process.stdin.resume();
process.stdin.on("data", function(text) {
               
//shows entered length=6
console.log('entered length=' + text.length); 

//shows compare length=4
console.log('compare length=' + 'exit'.length);

if (text == 'exit'){
 //never works
console.log('matched!');
}
});

mscdex

unread,
Aug 2, 2012, 9:32:56 AM8/2/12
to nodejs
On Aug 2, 9:15 am, Danil Gazizov <danil.gazi...@gmail.com> wrote:
> I'm confused of how to compare string const with variable.
> Look at this simple example where keyboard input compares with const string.
> I suggest this is encoding problem. Please, tell me what should be done to
> exit process ?

What is the output of `console.dir(text);` ?

Dan Milon

unread,
Aug 2, 2012, 9:24:57 AM8/2/12
to nod...@googlegroups.com
for me, text is "exit\n"
I guess you are on windows, so text is "exit\r\n"

The readline module might help you.

On 08/02/2012 04:15 PM, Danil Gazizov wrote:
> I'm confused of how to compare string const with variable.
> Look at this simple example where keyboard input compares with const
> string.
> I suggest this is encoding problem. Please, tell me what should be
> done to exit process ?
> File encoding is utf8
>
> process.stdin.setEncoding();//default is utf-8
> process.stdin.resume();
> process.stdin.on("data", function(text) {
>
> //shows entered length=*6*
> console.log('entered length=' + text.length);
>
> //shows compare length=*4*
> console.log('compare length=' + 'exit'.length);
>
> if (text == 'exit'){
> //never works
> console.log('matched!');
> }
> });
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Dominic Tarr

unread,
Aug 2, 2012, 11:17:52 AM8/2/12
to nod...@googlegroups.com
try removing whitespace from the ends

text = text.trim()

Dominic Tarr

unread,
Aug 2, 2012, 11:33:37 AM8/2/12
to nod...@googlegroups.com
try removing whitespace from the ends

text = text.trim()

On Thu, Aug 2, 2012 at 3:24 PM, Dan Milon <danm...@gmail.com> wrote:

Danil Gazizov

unread,
Aug 2, 2012, 12:30:30 PM8/2/12
to nod...@googlegroups.com
Oh.. things are more simple than I supposed! 
Thank's, Dan! You saved my brain 

четверг, 2 августа 2012 г., 17:24:57 UTC+4 пользователь Dan Milon написал:

Danil Gazizov

unread,
Aug 2, 2012, 12:31:31 PM8/2/12
to nod...@googlegroups.com
Thank's Dominic! It works!

четверг, 2 августа 2012 г., 19:17:52 UTC+4 пользователь Dominic написал:
>> nodejs+unsubscribe@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscribe@googlegroups.com
Reply all
Reply to author
Forward
0 new messages