From: macdevign@... Date: 2019-01-02T08:52:01+00:00 Subject: [ruby-core:90851] [Ruby trunk Feature#15496] Extract between string as standard String api Issue #15496 has been reported by macdevign (Macdevign mac). ---------------------------------------- Feature #15496: Extract between string as standard String api https://bugs.ruby-lang.org/issues/15496 * Author: macdevign (Macdevign mac) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I could not find the a simple String api to extract the string between two string, and I notice that many face the same issue and endup rolling their own solutions (eg https://stackoverflow.com/questions/9661478/how-to-return-the-substring-of-a-string-between-two-strings-in-ruby). Given that string "between" extraction is such a common operation, will adding a focused and simplified String method make coding pleasant ? This is my solution but probably someone can provide better and efficient implementation. ` # self: String instance, # from: first 'from' String # to: first 'to' String found after 'from' # return string between from and to, which exclude the argument, nil otherwise. def between(from, to) from_idx = self.index(from)&.+(from.length) if from_idx to_idx = self.index(to, from_idx) return self[from_idx...to_idx] if to_idx end nil end ` Test case "Hello world".between("ell", "ld") => "o wor" "Testing 123".between("Te", "123") => "sting " -- https://bugs.ruby-lang.org/ Unsubscribe: