Ruby字节数组转换为十六进制字符串

Python012

Ruby字节数组转换为十六进制字符串,第1张

str = "Ruby"

str.split(//).each {|e|print (e.unpack('H*').to_s + "\n")}

#irb

# 52

# 75

# 62

# 79

# =>["R", "u", "b", "y"]

这个是将一串字符串转化成数组的方法

(1) 在默认无参传入的时候 ,是以空格为间隔 ,获得数组

 pry(main)> " now's the time".split

 => ["now's", "the", "time"]

(2) 如果接受一个字符参数 ,那么会按照这个字符参数进行分割变成数组

pry(main)> "mellow yellow".split("ello")

 => ["m", "w y", "w"]

(3)如果接受的是两个参数 ,后面的参数是用来确定分割数组里面元素的个数,如果在按规则分割的时候 剩下多余的就变成一个元素

pry(main)> "mellow,yellow".split(//,4)

 => ["m", "e", "l", "low,yellow"]

(4)当然split 参数 也接受正则表达式,如下

pry(main)> "hi mom".split(%r{\s*})

 => ["h", "i", "m", "o", "m"]

c中是strstr.

ruby 1.9.3中包含:

include? other_str → true or false click to toggle source

Returns true if str contains the given string or character.

"hello".include? "lo" #=>true

"hello".include? "ol" #=>false

"hello".include? ?h #=>true