Dealing with non breaking space

by iFarmer Limited , 14 March

1 min read

Dealing with non breaking space

Recently we found a bug in our mobile app which is, comparing two variable returning false for few mobile while everything seems to be normal.After digging deep for 2 hours we found that the non breaking spacing is the culprit.Interestingly this problem does not occure for all of the mobile phone.We use unicode in our app.

 

#What is non-breaking space?

In word processing and digital type setting, a non-breaking space, also called NBSP, required space, hard space, or fixed space, is a space character that prevents an automatic line break at its position.

 

#The incident we faced

As we work with bengali font alot when you copy paste from somewhere like google translator there are higher chance to add a non breaking space. The problem is if the data you are inserting might be an operand hence your logical check will return false.

e.g

params[:product_type] == 'গরু' # will always returns false if there is non breaking space

 

#The Remeady

You can not replace this type of space in conventional way like params[:product_type].gsub(" ", "") == 'গরু' 

Because language can't determine if there is space or not, hence the general replace mechanism doesn't work for non breaking space.

Instead you have to use character code to replace the non breaking space

params[:product_type].gsub("\u00A0", "") == 'গরু'

and you are safe now tada!!!!!

!!! Happy coding !!!