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_HELO_NONE,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 9490F1F462 for ; Wed, 5 Jun 2019 08:06:11 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id ED6F5120937; Wed, 5 Jun 2019 17:06:06 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id D511F120939 for ; Wed, 5 Jun 2019 17:06:03 +0900 (JST) Received: by filter0109p3las1.sendgrid.net with SMTP id filter0109p3las1-13902-5CF777EC-2B 2019-06-05 08:06:04.859159404 +0000 UTC m=+52342.461674173 Received: from herokuapp.com (unknown [34.204.44.93]) by ismtpd0043p1mdw1.sendgrid.net (SG) with ESMTP id L9NVrsoiS_a62tzJG2BBOA for ; Wed, 05 Jun 2019 08:06:04.606 +0000 (UTC) Date: Wed, 05 Jun 2019 08:06:04 +0000 (UTC) From: sawadatsuyoshi@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68451 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15899 X-Redmine-Issue-Author: kke X-Redmine-Sender: sawa 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: =?us-ascii?Q?jFXA8Rt481sXUUIO9tYW1AJlMOZdNdlSw=2F5TfLCefGsQOqu40iuqkbxHm+3tBW?= =?us-ascii?Q?1paJ8YSrJ9wYUZsyNTWkIYH=2FDN2yEXxFbNRBMox?= =?us-ascii?Q?dAss0BIA6HIkVCz=2F4HUkz1zhseBuHEgA4OqmAwR?= =?us-ascii?Q?7AbC1Zx+iWUyVSxRb4aWto1v=2F3LT50jloCniUoO?= =?us-ascii?Q?ymAdFT32ShfbkVd5Tf6e6pQUaNDnf1UTWWQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92974 Subject: [ruby-core:92974] [Ruby trunk Feature#15899] String#before and String#after 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 #15899 has been updated by sawa (Tsuyoshi Sawada). Using `partition` looks reasonable, and it can accept regexes. ```ruby str = 'application/json; charset=utf-8' before, _, after = str.partition(/; /) before # => "application/json" after # => "charset=utf-8" ``` ---------------------------------------- Feature #15899: String#before and String#after https://bugs.ruby-lang.org/issues/15899#change-78351 * Author: kke (Kimmo Lehto) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- There seems to be no methods for getting a substring before or after a marker. Too often I see and have to resort to variations of: ``` ruby str[/(.+?);/, 1] str.split(';').first substr, _ = str.split(';', 2) str.sub(/.*;/, '') str[0...str.index(';')] ``` These create intermediate objects or/and are ugly. The `String#delete_suffix` and `String#delete_prefix` do not accept regexps and thus only can be used if you first figure out the full prefix or suffix. For this reason, I suggest something like: ``` ruby > str = 'application/json; charset=utf-8' > str.before(';') => "application/json" > str.after(';') => " charset=utf-8" ``` What should happen if the marker isn't found? In my opinion, `before` should return the full string and `after` an empty string. -- https://bugs.ruby-lang.org/