Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby, delete null values of a string

Tags:

string

null

ruby

Example:

String test="hi\000\000\000"

Problem: Some methods require a string to be without nulls, how can I delete all null values of a string?

.split("\000",1) gives me an error: 'force_encoding' method doesn't exist
.gsub('\000','') does nothing
like image 809
Onetimeposter123 Avatar asked Sep 23 '11 07:09

Onetimeposter123


3 Answers

Even more simple:

test.delete("\000")
like image 164
undur_gongor Avatar answered Oct 11 '22 08:10

undur_gongor


Try using double quotes, so test.gsub("\000", '').

like image 36
Adam Eberlin Avatar answered Oct 11 '22 09:10

Adam Eberlin


Right now I tried this in JRuby and it worked:

test.gsub(/\000/, '')

Note that I am using a regex in the gsub and not a string.

like image 32
Behrang Avatar answered Oct 11 '22 07:10

Behrang