From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.1 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id CC7111F770 for ; Wed, 2 Jan 2019 08:52:09 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C116A121992; Wed, 2 Jan 2019 17:52:06 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 4A2441211F7 for ; Wed, 2 Jan 2019 17:52:04 +0900 (JST) Received: by filter0056p3las1.sendgrid.net with SMTP id filter0056p3las1-24568-5C2C7BB0-D 2019-01-02 08:52:00.481449472 +0000 UTC m=+1074992.736686311 Received: from herokuapp.com (ec2-54-224-14-133.compute-1.amazonaws.com [54.224.14.133]) by ismtpd0045p1mdw1.sendgrid.net (SG) with ESMTP id J_4-ZIGpTb6b_IPrQpDsIw for ; Wed, 02 Jan 2019 08:52:00.372 +0000 (UTC) Date: Wed, 02 Jan 2019 08:52:01 +0000 (UTC) From: macdevign@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66275 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15496 X-Redmine-Issue-Author: macdevign X-Redmine-Sender: macdevign X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS5suRE+g0R1iyIGBMqXoIVM5tqee77CCqozhE nVHlOyTsswy6OukaPNhU7WarbZNFn+8r7cXYkOgFwfA83rUzx0TAEi+9sBYEPDlYxhnUmE0YzThDSm GXGeyHRhorIPIiWy/o1QkJZ7ID1BNs+YLspbFnlz1pRdpBnAV38JcjC6mQ== X-ML-Name: ruby-core X-Mail-Count: 90851 Subject: [ruby-core:90851] [Ruby trunk Feature#15496] Extract between string as standard String api X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" 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/