ruby当中的字符串处理%Q, %q, %W, %w, %x, %r, %s 的用法

%Q

This is an alternative for double-quoted strings, when you have more quote characters in a string.Instead of putting backslashes in front of them, you can easily write:
>> %Q(Joe said: "Frank said: "#{what_frank_said}"")
=> "Joe said: "Frank said: "Hello!"""

The parenthesis “()” can be replaced with any other non-alphanumeric characters and non-printing characters (pairs), so the following commands are equivalent:
>> %Q!Joe said: "Frank said: "#{what_frank_said}""!
>> %Q[Joe said: "Frank said: "#{what_frank_said}""]
>> %Q+Joe said: "Frank said: "#{what_frank_said}""+

You can use also:
>> %/Joe said: "Frank said: "#{what_frank_said}""/
=> "Joe said: "Frank said: "Hello!"""

%q

Used for single-quoted strings.The syntax is similar to %Q, but single-quoted strings are not subject to expression substitution or escape sequences.
>> %q(Joe said: ‘Frank said: ‘#{what_frank_said} ‘ ‘)
=> "Joe said: ‘Frank said: ‘\#{what_frank_said} ‘ ‘"

字符串的切分,将已有的字符串切分为多个数组

%W

Used for double-quoted array elements.The syntax is similar to %Q
>> %W(#{foo} Bar Bar\ with\ space)
=> ["Foo", "Bar", "Bar with space"]

%w

Used for single-quoted array elements.The syntax is similar to %Q, but single-quoted elements are not subject to expression substitution or escape sequences.
>> %w(#{foo} Bar Bar\ with\ space)
=> ["\#{foo}", "Bar", "Bar with space"]

%x

Uses the ` method and returns the standard output of running the command in a subshell.The syntax is similar to %Q.
>> %x(echo foo:#{foo})
=> "foo:Foo\n"

%r

Used for regular expressions.The syntax is similar to %Q.
>> %r(/home/#{foo})
=> "/\\/home\\/Foo/"

%s

Used for symbols.It’s not subject to expression substitution or escape sequences.
>> %s(foo)
=> :foo
>> %s(foo bar)
=> :"foo bar"
>> %s(#{foo} bar)
=> :"\#{foo} bar"

时间: 2024-10-10 20:33:09

ruby当中的字符串处理%Q, %q, %W, %w, %x, %r, %s 的用法的相关文章

Ruby 里的 %Q, %q, %W, %w, %x, %r, %s, %i (译)转

原文地址  转自 %Q 用于替代双引号的字符串. 当你需要在字符串里放入很多引号时候, 可以直接用下面方法而不需要在引号前逐个添加反斜杠 (\") >> %Q(Joe said: "Frank said: "#{what_frank_said}"") => "Joe said: "Frank said: "Hello!""" (...)也可用其他非数字字母的符号或成对的符号代替,

Crosswalk Cordova windows下编译出现:bin\node_modules\q\q.js:126 throw e问题解决方法

Crosswalk Cordova windows下编译出现:bin\node_modules\q\q.js:126   throw e问题解决方法 Crosswalk Cordova 编译出现以下问题 D:\code\crosswalk\crosswalk-cordova-10.39.235.15-arm\bin\node_modules\q\q.js:126 throw e; ^ Package name must look like: com.company.Name 原因是:create

l?e?f?t? ?j?o?i?n? ?o?n? ?a?n?d?与?l?e?f?t? ?j?o?i?n? ?o?n? ?w?h?e?r?e?的?区?别(转载)

数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1.on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录. 2where条件是在临时表生成好后,再对临时表进行过滤的条件.这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉. 假设有两张表: 表1 tab1: id size 1 10 2 20 3 30 表2 ta

ruby 中%Q %q %W %w %x %r %s的用法

%Q 用于替代双引号的字符串. 当你需要在字符串里放入很多引号时候, 可以直接用下面方法而不需要在引号前逐个添加反斜杠 (\") >> %Q(Joe said: "Frank said: "#{what_frank_said}"") => "Joe said: "Frank said: "Hello!""" (...)也可用其他非数字字母的符号或成对的符号代替, 诸如[...],

Ruby 里的 %Q, %q, %W, %w, %x, %r, %s, %i

%Q 用于替代双引号的字符串. 当你需要在字符串里放入很多引号时候, 可以直接用下面方法而不需要在引号前逐个添加反斜杠 (\") >> %Q(Joe said: "Frank said: "#{what_frank_said}"") => "Joe said: "Frank said: "Hello!""" (...)也可用其他非数字字母的符号或成对的符号代替, 诸如[...],

# ruby的gsub字符串替换功能

1.可以使用hash来替换对应的字符串: "hello 123 world".gsub(/hello|world/, 'hello'=>'HELLO', 'world'=>'WORLD') # => "HELLO 123 WORLD" 虽然支持第二个参数为hash,但不支持 symbol索引,即 hello: 'HELLO', world: 'WORLD' 无效 "hello 123 world".gsub(/hello|wor

ruby随机生成字符串

随机生成一个固定位数的字符串: def newpass( len ) chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a newpass = "" 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] } return newpass

python 读写文件中 w与wt ; r与rt 的区别

w,r,wt,rt都是python里面文件操作的模式. w是写模式,r是读模式. t是windows平台特有的所谓text mode(文本模式),区别在于会自动识别windows平台的换行符. 类Unix平台的换行符是\n,而windows平台用的是\r\n两个ASCII字符来表示换行,python内部采用的是\n来表示换行符. rt模式下,python在读取文本时会自动把\r\n转换成\n. wt模式下,Python写文件时会用\r\n来表示换行.

qa:c#文本字符串插入双引号:@用法和反斜杠用法

Excel中添加超链接,可以用hyperlink公式,即=hyperlink(src,display); 在stackoverflow中的示例中:http://stackoverflow.com/questions/2905633/add-hyperlink-to-cell-in-excel-2007-using-open-xml-sdk-2-0 CellFormula cellFormula1 = new CellFormula() { Space = SpaceProcessingModeV