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

  • Home
  • SEARCH
  • 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 9134103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:36:08+00:00 2026-06-17T08:36:08+00:00

I want to search through an array using a partial string, and then get

  • 0

I want to search through an array using a partial string, and then get the index where that string is found. For example:

a = ["This is line 1", "We have line 2 here", "and finally line 3", "potato"]
a.index("potato") # this returns 3
a.index("We have") # this returns nil

Using a.grep will return the full string, and using a.any? will return a correct true/false statement, but neither returns the index where the match was found, or at least I can’t figure out how to do it.

I’m working on a piece of code that reads a file, looks for a specific header, and then returns the index of that header so it can use it as an offset for future searches. Without starting my search from a specific index, my other searches will get false positives.

  • 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-06-17T08:36:09+00:00Added an answer on June 17, 2026 at 8:36 am

    Use a block.

    a.index{|s| s.include?("We have")}
    

    or

    a.index{|s| s =~ /We have/}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to search through a html file and then get the url to
I'm trying to search through this Json array using PHP: {count:2,items:{milestone:[{id:3107,username:TomSmith1,userid:1620602,date:2012-01-12 16:49:26,projectid:804,projectname:TEST PROJECT,reason:Partial payment
I want to search two item (name=string and location=json). this search is (one input
Here's my search array: $aExt = array('png','jpg','gif'); I want to search through the variable:
I want to filter and search through Json using multiple keywords. I have it
Similar Stack Overflow Question I want users to be able to search through my
We want to search a file to find all instances of bar that are
I want to search a text in a table without knowing its attributes. Example
I've read a file into an array of characters using fread. Now I want
I have a json object array. I want to search the array and for

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.