Click here to Skip to main content
15,905,071 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I am getting an output on telnet console like below,


----------------(line1)
THURSDAY 88669(line2)
----------------(line3)
Please enter date(line4)

I want to read the string between line1 and line3 Using C# code.

can indexof method help to find the string on line 2

What I have tried:

substring method checked .
Posted
Updated 8-Apr-16 3:06am
Comments
CHill60 8-Apr-16 8:53am    
Share the code you used

Well, maybe - but it depends on what you are doing when you read the data.
If you use File.ReadAllLines, then each line from teh file will be in a separate element of the array, so this would work:
C#
string[] lines = File.ReadAllLines(path);
if (lines.Length >= 3)
    {
    string line2 = lines[1];
    //...
    }
But...that doesn't check that the data is what you think it is!
You might want to consider a regex?

[edit]
If you do want a regex, then this may work:
C#
string data = File.ReadAllText(path);
Match m = Regex.Match(data, @"(?<=-+\r\n).*?(?=\r\n-+\r\n)", RegexOptions.Multiline);
if (m.Success)
    {
    string line = m.Value;
    ...
    }


[/edit]
 
Share this answer
 
v2
Comments
nikhil2015 18-Apr-16 8:50am    
string s="----------------(line1)
THURSDAY 88669(line2)
----------------(line3)
Please enter date"(line4)

i am getting string in lines mentioned in quotes. From that string i need to extract 5 digit number.
OriginalGriff 18-Apr-16 9:05am    
And?
Where are you stuck?

And if you want help on something, tell us exactly what you want help with in teh first place!
nikhil2015 19-Apr-16 4:31am    
input is not reading from text file. All the lines are read from a console and store into a string s. from this string i need to extract line between Hyphens shown above.

do you have any code using indexoff or split method
OriginalGriff 19-Apr-16 5:22am    
It doesn't matter where the data is read from: console, file, database, ...
The code is the same.
Did you try what we suggested?
Read a line, and check if it is all hyphens. Then read the next line and use String.Split to separate the day from the date. You can repeat that through the file if there are more entries.
 
Share this answer
 
Comments
Richard MacCutchan 18-Apr-16 9:05am    
No it is not in quotes. And I already told you how to split the string.
Richard MacCutchan 18-Apr-16 9:26am    
Yes I could, but you will learn much more by going to the documentation, studying it, and trying for yourself. Reading from a text file and splitting strings are basic issues that you really need to master if you plan on a career in IT.
Richard MacCutchan 19-Apr-16 9:20am    
Why do you refuse to go and read the documentation? If you really want to make it in this industry then you need to be prepared to find things for yourself and try out different approaches. This issue is quite simple and all you need to do is to go to the MSDN documentation and read the section on the Split method in the String class. It will take you all of 10-15 minutes to learn what to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900