Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have text file as below,

One: 100
Two: 200
Three: 300

One: 1
Two: 2
Three: 3

One: 1000
Two: 2000
Three: 3000

One: 1100
Two: 2200
Three: 3300

I need to split this file and and put each block(One,tow and Three) in collection.

I have tried with Regex.Split



Please suggest.
Posted
Comments
T Pat 13-Nov-13 3:34am    
Are you successfully reading the text file?
Can you please be little more descriptive
yrishi 13-Nov-13 3:41am    
Yes I am able to read text file. After reading I am putting text into string variable and after that I need separate each block

Since your file format is 'pretty regular' you don't even need Regex. You might separate each block just using the blank line as separator. Then (if you need just the numbers) you might process each block via String.Split (passing a blank or the colon character).
 
Share this answer
 
Comments
yrishi 13-Nov-13 3:45am    
Tried with blank line as separator but no success. like String.Split(' ') :(
Hi YRishi,

You can split the text file using page break character.

Try this snippet for this
C#
StreamReader sr = new StreamReader(path)
string text = sr.ReadToEnd();
string[] lines = text.Split('\r');
foreach(string s in lines)
{
   // Consume
}

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
Comments
yrishi 14-Nov-13 0:56am    
Thanks RK.
♥…ЯҠ…♥ 14-Nov-13 0:58am    
You are welcome.....
♥…ЯҠ…♥ 14-Nov-13 5:27am    
Dont you think this solution can be Up-voted?
As you stated you are able to get the text data in string variable you try to split the string

C#
string[] splitter = { "One" };
string[] str = segment.Split(splitter, StringSplitOptions.None);


You can try something like above code. Hope this helps you, might need to tweak to et actual result.

Happy Coding :)
 
Share this answer
 

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