[英]How to insert bullet character in a Ruby string?
I'm trying to create a Ruby 1.9.3 string with a bullet character in it. 我正在尝试创建一个带有子弹字符的Ruby 1.9.3字符串。
str = "•" + " hello world"
But, when I type it in, I get a syntax error about a non-ASCII character. 但是,当我输入时,我得到一个关于非ASCII字符的语法错误。
How can I do this? 我怎样才能做到这一点?
您可以将Unicode字符放在那里。
str = "\u2022" + " hello world"
Works for me (ruby 1.9.3) 适合我(红宝石1.9.3)
str = "•" + " hello world"
str # => "• hello world"
Don't use ASCII string–but you don't state what version of Ruby you're using. 不要使用ASCII字符串 - 但是你没有说明你正在使用的Ruby版本。 You can also use the Unicode character,
"\•"
, if you're using 1.8, or see other options in the primer below. 如果您使用的是1.8,也可以使用Unicode字符
"\•"
,或者在下面的入门中查看其他选项。
In 1.9 this should work fine as-is. 在1.9中,这应该可以正常工作。 You can also force UTF-8 using
encode
and/or force_encoding
. 您还可以使用
encode
和/或force_encoding
强制使用UTF-8。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.