Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:34:46+00:00 2026-05-29T22:34:46+00:00

I want to know what’s the best way to make the String.include? methods ignore

  • 0

I want to know what’s the best way to make the String.include? methods ignore case. Currently I’m doing the following. Any suggestions? Thanks!

a = "abcDE"
b = "CD"
result = a.downcase.include? b.downcase

Edit:
How about Array.include?. All elements of the array are strings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:34:47+00:00Added an answer on May 29, 2026 at 10:34 pm

    Summary

    If you are only going to test a single word against an array, or if the contents of your array changes frequently, the fastest answer is Aaron’s:

    array.any?{ |s| s.casecmp(mystr)==0 }
    

    If you are going to test many words against a static array, it’s far better to use a variation of farnoy’s answer: create a copy of your array that has all-lowercase versions of your words, and use include?. (This assumes that you can spare the memory to create a mutated copy of your array.)

    # Do this once, or each time the array changes
    downcased = array.map(&:downcase)
    
    # Test lowercase words against that array
    downcased.include?( mystr.downcase )
    

    Even better, create a Set from your array.

    # Do this once, or each time the array changes
    downcased = Set.new array.map(&:downcase)
    
    # Test lowercase words against that array
    downcased.include?( mystr.downcase )
    

    My original answer below is a very poor performer and generally not appropriate.

    Benchmarks

    Following are benchmarks for looking for 1,000 words with random casing in an array of slightly over 100,000 words, where 500 of the words will be found and 500 will not.

    • The ‘regex’ text is my answer here, using any?.
    • The ‘casecmp’ test is Arron’s answer, using any? from my comment.
    • The ‘downarray’ test is farnoy’s answer, re-creating a new downcased array for each of the 1,000 tests.
    • The ‘downonce’ test is farnoy’s answer, but pre-creating the lookup array once only.
    • The ‘set_once’ test is creating a Set from the array of downcased strings, once before testing.
                    user     system      total        real
    regex      18.710000   0.020000  18.730000 ( 18.725266)
    casecmp     5.160000   0.000000   5.160000 (  5.155496)
    downarray  16.760000   0.030000  16.790000 ( 16.809063)
    downonce    0.650000   0.000000   0.650000 (  0.643165)
    set_once    0.040000   0.000000   0.040000 (  0.038955)
    

    If you can create a single downcased copy of your array once to perform many lookups against, farnoy’s answer is the best (assuming you must use an array). If you can create a Set, though, do that.

    If you like, examine the benchmarking code.


    Original Answer

    I (originally said that I) would personally create a case-insensitive regex (for a string literal) and use that:

    re = /\A#{Regexp.escape(str)}\z/i # Match exactly this string, no substrings
    all = array.grep(re)              # Find all matching strings…
    any = array.any?{ |s| s =~ re }   #  …or see if any matching string is present
    

    Using any? can be slightly faster than grep as it can exit the loop as soon as it finds a single match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want know is there any way to configure a SVN over a LAN
I am currently dabbling in Magento and I want know how I can create
want to know why String behaves like value type while using ==. String s1
I want know is there good way to detect Column DataType for Date field
I want know, what regular expression should I have for my string. My string
Hi friends i want know sectionIndexTitlesForTableView Action is works i have the following code...
Want know any external API web service or PHP libraries that will allow me
I want know to make a query using linq, between a collection of objects
I want know what is the best : Array OR Binary search tree in
I'm building a radio application on android , I want know how to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.