views:

23

answers:

3

I've seen this posted a couple of times but none of the solutions seem to work for me so far...

I'm trying to remove a spurious  character from a string...

e.g.

"myÂstring here Â$100"

should be "my string here $100"

I've tried

string.gsub(/\194/,'')
string.gsub(194.chr,'')
string.delete 194.chr

All of these still leave the  intact..

any thoughts?

A: 
"myÂstring here Â$100".gsub("Â","") # "mystring here $100"

Is that what you meant?

zetetic
I'd also tried that, but to no avail..
easyjo
+1  A: 

By default, Rails supports UTF-8.

You can use your favorite editor to write a gsub call using the proper character you want to replace, as in:

"myÂstring here Â$100".gsub(/Â/,"")

If this does not work as well, you might be having an encoding error somewhere on your stack, probably on your HTML document. Try running rails console, extract somehow that string (if it comes from the Model, try to perform a find on the containing class) and run the gsub. It won't solve your problem, but you'll get a clue to where exactly the problem may lie.

Fábio Batista
pretty sure this has resolved it thanks, I had tried .gsub("Â","") rather than .gsub(/Â/,"") which I had assumed would work the same way... just going to try a few tests to see if this has resolved it
easyjo
+1  A: 

Looks like a character encoding problem to me. For every Unicode code point in the range U+0080..U+00BF inclusive, the UTF-8 encoding is a two-byte sequence, 0xC2 (194 decimal) and the numeric value the code point. For example, a non-breaking space--U+00A0--becomes 0xC2 0xA0. Was there another extra character in there, that you already removed?

At any rate, gsub(/\194/,'') is wrong. \nnn is supposed to be an octal escape, but the number is in its decimal form. 194 in octal is \302.

Alan Moore