问题描述
我很想得到纯文字,但是我感到困惑和困惑。
“汽车和小型货车\ n \ t \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t \ t \ t卡车\ n \ t \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t \ t跨界车和SUV \ n \ t \ t \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t \ t \ t混合动力和燃料电池\ n \ t \ t \ t \ t \ t \ t \ n \ n \ t \ t \ t \ t \ n \ t \ t \ t \ t \ t即将到来的车辆“
解决方法
如果str
是您的字符串,也许您只是想
str.delete("\n\t")
#=> "Cars & Minivan Trucks Crossovers & SUVs Hybrids & Fuel Cell Upcoming Vehicles"
请注意,String#delete不会修改str
。
\n
是换行符(表示按Enter键以换行),而\t
是制表符。您可以将字符串拆分为这些字符以摆脱它们(假设您的字符串为s
):s.split(/\n|\t/)
。传递给split的东西称为正则表达式。
这将在列表中留下很多空字符串和空格。因此,您想使用的是cars = s.split(/\n|\t/).select {|c| c.length > 1}