Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a null character from a string in javascript

How can you remove null bytes from a string in nodejs?

MyString\u0000\u0000\u00000 

I tried string.replace('\0', ''); but it doesn't seem to work. Is there any node package that is good for maniputing strings?

like image 793
majidarif Avatar asked Apr 02 '14 11:04

majidarif


People also ask

How do I remove a null character from a string?

line = line. rstrip() will do the job. Unlike strip(), rstrip() only removes trailing whitespace characters. I tried it with null spaces on REPL and it worked.

How do I remove a character from a string in Javascript?

To remove a character from a string, use string replace() and regular expression. This combination is used to remove all occurrences of the particular character, unlike the previous function. A regular expression is used instead of a string along with global property.

What is u0000 in Javascript?

The string \u0000 is the Unicode character for NULL . It corresponds to the number 0 .

WHAT IS NULL character in Javascript?

Therefore, a NUL byte should simply be "yet another character value" and have no special meaning, as opposed to other languages where it might end a SV (String value). For Reference of (valid) "String Single Character Escape Sequences" have a look at the ECMAScript Language spec section 7.8.


1 Answers

It works, but like this it only removes 1 instance of a null-byte. You need to do it with Regular Expressions and the g modifier

var string = 'MyString\u0000\u0000\u0000'; console.log(string.length); // 11 console.log(string.replace('\0', '').length); // 10 console.log(string.replace(/\0/g, '').length); // 8 
like image 79
akirk Avatar answered Oct 09 '22 14:10

akirk