如何将字符代码转换为我想要的? [英] How to convert character code to what I want?

查看:35
本文介绍了如何将字符代码转换为我想要的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样做:

s = "hello"
s[0]

输出字符代码值104.

我需要做什么来转换 104 使其输出h"?

What do I need to do to convert the 104 such that it outputs 'h'?

我使用的是 ruby​​ 1.8.7.

I am using ruby 1.8.7.

推荐答案

您的一些选择包括:

  • 使用s[0].chr
  • 使用 s[0...1]s[0,1]
  • 升级到 Ruby 1.9,其中字符串不再被视为字节数组,并且 s[0] 按预期为您提供 "h".
  • Use s[0].chr
  • Use s[0...1] or s[0,1]
  • Upgrade to Ruby 1.9 where strings are no longer considered arrays of bytes, and s[0] gives you "h" as expected.

这篇关于如何将字符代码转换为我想要的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆