ruby,如何删除字符串的第一部分?

时间:2018-02-27 14:26:40

标签: ruby

这是我第一次在这个论坛上发帖,我正在做一些selenium webdriver测试,我从一个html UI中收集了一些数据,把它放在一个数组中:

consents = $driver.find_elements(:xpath,"//*[@id='main-display']/div[2]/div[2]/div/table/tbody/tr/td[7]//i")

,然后我用map:

处理了数组
consent_values = consents.map { |consent| consent.attribute('class')}

打印我得到的数组:

["fa fa-check-circle", "fa fa-circle-thin", "fa fa-check-circle", "fa fa-circle-thin", "fa fa-circle-thin", "fa fa-check-circle", "fa fa-circle-thin", "fa fa-circle-thin", "fa fa-check-circle,....]

1)有没有办法在地图中删除" fa"来自数组中的所有字符串?

2)如何更改字符串" fa fa-check-circle"是真实的" fa fa-circle-thin"是假的所以我会得到: [真,假真,...]

谢谢,

2 个答案:

答案 0 :(得分:1)

基本上2)消除了1)的必要性,所以在这里你去:

consent_values = consents.map do |consent|
  case consent.attribute('class')
  when "fa fa-check-circle" then true
  when "fa fa-circle-thin" then false
  else nil
  end
end

要从字符串中删除"fa "前缀,可以使用String#[]

"fa fa-check-circle"[/(?<=\Afa ).*/]
#⇒ "fa-check-circle"

答案 1 :(得分:0)

string[N..-1]

这对我有用。谢谢,无论如何,我希望我会与这个论坛很多人互动。