473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to search for numbers in txt file C# ?

I open a .txt file and read numbers separated by enters or spaces into an array list.


Now what I want to do is to search (for 1 2 9 ) and send to the console.
I have tried a lot of code but nothing seems to work :(

This is my current code :
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace Padroes
  8. {
  9. class Program
  10. {
  11.     static void Main(string[] args)
  12.     {
  13.         try
  14.         {
  15.  
  16.  
  17.             // Open the text file using a stream reader.
  18.             const string FILENAME = @"Example.txt";
  19.  
  20.  
  21.                 List<List<int>> data = new List<List<int>>();
  22.  
  23.                 string inputLine = "";
  24.                 StreamReader reader = new StreamReader(FILENAME);
  25.  
  26.                 while ((inputLine = reader.ReadLine()) != null)
  27.                 {
  28.                     inputLine = inputLine.Trim();
  29.                     if (inputLine.Length > 0)
  30.                     {
  31.                         List<int> inputArray = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
  32.  
  33.                         data.Add(inputArray);
  34.                     Console.WriteLine(inputLine);
  35.                     }
  36.                 }
  37.             }
  38.  
  39.         catch (Exception e)
  40.         {
  41.             Console.WriteLine("The file could not be read:");
  42.             Console.WriteLine(e.Message);
  43.         }
  44.  
  45.         Console.ReadKey();
  46.     }
  47. }
  48. }
Whith this code this is my output:


Now what can I do to search only for ( 1 2 9 ) and send only the (1 2 9) to console ?
Oct 26 '15 #1
1 2299
adriancs
122 100+
Expand|Select|Wrap|Line Numbers
  1. if (inputline.Trim() == "1 2 9")
  2. {
  3.     // do something
  4. }
Dec 23 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: collinm | last post by:
i try to search txt file in a folder my code int searchfile(char *dir, char *filename, int flength, char *ext) { DIR *pdir; pdir = opendir(dir); struct dirent *pent; if (!pdir)
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
4
by: larry | last post by:
Hi, I would like to use window search to search a word in C# CS file which create by visual studio. But I can never get that word in the cs file. for example, I used a name space:...
5
by: Stewart Saathoff | last post by:
Hello, I am trying to search the file system from a string of text located inside of a textbox on a form and have it return a list of file names in a list box on that same form. Does anyone...
5
by: anilreddy_3 | last post by:
write a program that search a file for a keyword and replace that keyword with a another word. e.g. search for 'have' and replace with 'had' and list all the changes made ....to the screen...
0
by: pa2ratczi | last post by:
Hi there i need an urgent help for those guys who has a broad knowledge about Vb6, i have a project in vb which is i have to make a program that search a file in a local hard disk, with just using...
2
by: princymg | last post by:
I want to search a file from server and want to copy it to the local disk. how it is done? This is working if the file is in my hard disk itself.But not when it comes to server. If i map the server...
2
by: princymg | last post by:
I want to search a file from server and want to copy it to the local disk. how it is done? This is working if the file is in my hard disk itself.But not when it comes to server. If i map the server...
7
Neo69ADSL
by: Neo69ADSL | last post by:
Hello to all. First of all, if there is any mistakes in writting, i'm sorry but i'm portuguese and my english isn't very good. This is my problem. I'm making an Access Database and i'm trying to...
4
by: FraterVL | last post by:
I am currently working on an assignment for school. I am writing a grade book program. So far I have written code to show all current grades from a file, input new grades to the file and exiting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.