解释一下,就像我真的是五个..."format"字符串是什么意思?



当你创建一个字符串…"格式化"这个字符串意味着什么?

挑战:把我当白痴一样解释给我听。把它提升到居高临下的程度。刻薄一点。我说的是你如何向一个非常非常愚蠢的孩子解释一个柠檬水摊。

这个短语最常见的用法是指用变量内容的正确字符串表示替换字符串中的变量占位符。

考虑:

temp_reading = 25.67528
puts "It is currently %0.1f degrees" % [temp_reading]
-> It is currently 25.7 degrees

字符串格式化是将模板转换为您在输出中看到的字符串。

正如Phil Taprogge所指出的,格式化字符串通常指的是出于表示原因而改变数据的表示。

菲尔的例子
long_number = 1.11111111111111111111111111111111111111111111111111111111111
puts "%0.1f" % long_number
=> 1.1
puts "%d" % long_number
=> 1

有大量关于字符串格式化的文档,通常它可以延续到不同的语言,因为它来自C编程语言printf

格式化字符串可以引用对字符串的任何和所有转换来表示。

str = "hello world"
str.downcase
=> "hello world"
str.upcase
=> "HELLO WORLD"
str.capitalize
=> "Hello world"
str.titlieze
=> "Hello World"
str.parameterize
=> "hello-world"
https://blog.udemy.com/ruby-sprintf/

http://www.cplusplus.com/reference/cstdio/printf/

最新更新