Remove first 4 characters from string

Through a bunch of statements, if

I am concatenating a string to be used as an SQL statement. The first 3 characters of this line will always be "OR". How can I efficiently remove those first 4 characters.

example:
      sql = " OR tennis = TRUE OR basetball = TRUE"
      if condition sql = sql + " OR racquetball = TRUE"

      

So I need to remove the "OR" and 2 spaces at the beginning of any line.

thank you for your time

+3


source to share


1 answer


It would be better to just concatenate everything together at the end:

sql = [ ]

sql << 'tennis = TRUE'
sql << 'baseball = TRUE'

if (condition)
  sql << 'racquetball = TRUE'
end

sql = sql.join(' OR ')

      

You can also reduce redundancy by reassigning things:



sports = [ :tennis, :baseball ]

sql = sports.collect { |s| "#{s} = TRUE" }.join(' OR ')

      

If you intend to trim the first four letters:

sql = " OR x OR y"
sql.slice!(" OR ")
sql
# => "x OR y"

      

+8


source







All Articles