简体   繁体   中英

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.

str = "•" + " hello world"

But, when I type it in, I get a syntax error about a non-ASCII character.

How can I do this?

您可以将Unicode字符放在那里。

str = "\u2022" + " hello world"

Works for me (ruby 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. You can also use the Unicode character, "\•" , if you're using 1.8, or see other options in the primer below.

In 1.9 this should work fine as-is. You can also force UTF-8 using encode and/or force_encoding .

Ruby 1.9 Encodings Primer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM