【问题标题】:Confusion with string#concat(integer) method's output与 string#concat(integer) 方法的输出混淆
【发布时间】:2013-01-19 17:15:55
【问题描述】:

我正在测试String#concat(integer) 方法。代码如下:

irb(main):006:0> a="hello"
=> "hello"
irb(main):008:0> a<< "world"
=> "helloworld"
irb(main):009:0> a.concat(33)
=> "helloworld!"
irb(main):010:0> a.concat(32)
=> "helloworld! "
irb(main):011:0> a.concat(31)
=> "helloworld! \x1F"
irb(main):012:0> a.concat(34)
=> "helloworld! \x1F\""
irb(main):013:0> a.concat(3)
=> "helloworld! \x1F\"\x03"
irb(main):014:0>

但无法理解——为什么33 的值会给出helloworld! 的输出(正确,但不是其他整数)?

32 为什么/如何给出输出"helloworld! "?

a &lt;&lt; "world" 以什么方式在内部生成字符串?

【问题讨论】:

  • Ascii 33 是​​! 32 是空格???
  • documentation 表示:If the object is a Integer, it is considered as a codepoint, and is converted to a character before concatenation. 也许“代码点”会让您感到困惑,所以请在 en.wikipedia.org/wiki/Code_point 和 inamidst.com/stuff/unidata 阅读更多内容
  • @theTinMan 谢谢先生,我会的,但是第三个链接太多了! :(
  • @theTinMan 只是提出一个建议,在文档中有一些模块没有提到单个代码,一些方法也没有描述。作为初学者,我应该如何开始测试它们以查看它们的工作方式 - 知道吗?以ENV 为例

标签: ruby


【解决方案1】:

它给你helloworld!,因为! 标记的ASCII 码是33。 Link.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多