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 8427107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:42:31+00:00 2026-06-10T04:42:31+00:00

Is it possible to search a TextFile line by line to find a specific

  • 0

Is it possible to search a TextFile line by line to find a specific string.
ie. Hello
It searches line by line to find if any line has Hello in it. There will only be one string per line.
Is this possible?
If so how do I attempt this?

  • 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-10T04:42:33+00:00Added an answer on June 10, 2026 at 4:42 am

    It’s certainly easiest to load the entire file into memory. Provided that your file is small enough then you can do it like this:

    found := false;
    sl := TStringList.Create;
    try
      sl.LoadFromFile(fileName);
      for line in sl do
        if Pos('Hello', line)<>0 then
        begin
          found := true;
          break;
        end;
    finally
      sl.Free;
    end;
    

    I assume that when you say

    if any line has Hello in it

    that you are looking for lines that contain the search string rather than lines that equal the search string.


    In a comment you ask:

    Ok can I then ask if it would be possible to ask on how to
    delete a string that is typed from an edit box out of a file? So
    you enter a string into an edit box then it searches the file
    for it and deletes that line?

    That’s an easy enough variation of the above:

    procedure RemoveLinesContaining(const fileName, searchText: string);
    var
      sl: TStringList;
      i: Integer;
    begin
      sl := TStringList.Create;
      try
        sl.LoadFromFile(fileName);
        for i := sl.Count-1 downto 0 do
          if Pos(searchText, sl[i])<>0 then
            sl.Delete(i);
        sl.SaveToFile(fileName);
      finally
        sl.Free;
      end;
    end;
    

    This function deletes all lines that contain the search string. If you only want to delete the first such line, then break out of the for loop after the call to Delete.

    The loop variable is descending to allow the loop to modify the list.

    If you want to use a different test, say equality rather than contains, then simply modify the if statement.

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

Sidebar

Related Questions

Is it possible to search a string that is a regex, without escaping all
Is it possible to find (search) in Dynamics AX 2009 for an exact match?
Possible Duplicate: How to search for string in MS Access VBA array I am
Is it possible to search and replace all occurrences of a string in all
Users can do advanced searches (they are many possible parameters): /search/?query=toto&topic=12&minimumPrice=0&maximumPrice=1000 I would like
Possible Duplicate: How to search for a string in files with Ant (make sure
Is it possible to search zipped folders for a specific file name like this?
Is it possible to search through a MySQL database using blob data to find
How would it be possible to search for a string e.g. #Test1 in a
how to (if even possible) search for a string in a file including all

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.