How can I get the first character of a string that works both in ruby 1.8 (bytes) and 1.9 (characters)?
Another solution
"string"[0].chr # => 's'
This should do it...
s[0,1]
This returns the first byte in 1.8 and the first character in 1.9, but in each case the result is a String.
If you want the first UTF-8 character sequence in both, it's tricky. The regex engine is one place in 1.8 with UTF-8 awareness, so you could use:
s[/./u]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With