字符串转成字符数组:
>>str = "ABC"
=>"ABC"
>>chars = str.scan(/./)
=>["A", "B", "C"]
函数给你个python的参考吧,话说我python居然还真没忘。。。
'''正向操作
def f(x):
s = ""
for i in range(len(x)):
if i % 2 == 0:
s = s + ' '
s = s + x[i]
return s[1:]
'''逆向操作
def f_rev(x):
s = ""
'''凡是偶数位的空格删掉
'''
for i in range(len(x)):
if (i-2) % 3 == 0 and x[i] == ' ':
continue
s += x[i]
return s
'''测试
s = "abcdedg"
print (f_rev(f(s))
ruby的String类有一个方法叫chomp,用来去掉字符串末尾的\n或\r例子这样
"hello".chomp #=>"hello"
"hello\n".chomp #=>"hello"
"hello\r\n".chomp #=>"hello"
"hello\n\r".chomp #=>"hello\n"
"hello\r".chomp #=>"hello"