Posted By

joshmn on 04/22/13


Tagged

rails ruby String between


Versions (?)

Get string between two strings


 / Published in: Ruby
 

If you're using Rails and want to make this a global function, drop this into your application_controller and call it as a function.

Example: # getstringbetween("hellomyfriendmoto", 'o', 'm') # > myfriend

  1. def get_string_between(my_string, start_at, end_at)
  2. my_string = " #{my_string}"
  3. ini = my_string.index(start_at)
  4. return my_string if ini == 0
  5. ini += start_at.length
  6. length = my_string.index(end_at, ini).to_i - ini
  7. my_string[ini,length]
  8. end

Report this snippet  

You need to login to post a comment.