Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can anyone help me out with the syntax of reading a text file to an array with a delimiter " "(Empty space).
Posted
Updated 20-Jul-11 21:55pm
v2
Comments
Sergey Alexandrovich Kryukov 21-Jul-11 3:55am    
Removed unrelated tag ASP.NET.
--SA

This can be a complex as also an easy solution.

If you file is a small one, then a solution can be very basic

string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
string[] splittedText = text.Split(' ');


or even in one line
string[] splittedText = File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt").Split(' ');


Otherwise, be more precise on the request and I will point you to the proper direction.

Cheers

EDIT:

Based on the comments here are two quick solutions,

first one, if you are not managing the possible conversion errors
C#
string[] splittedText = File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt").Split(' ');
int[] numbersArray = Array.ConvertAll<string, int>(splittedText, delegate(string str) { return int.Parse(str); });


But as already mentioned, this wel generate an exception if there is a string that can't be parsed...

Or a more orthodox way:
C#
string[] splittedText = File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt").Split(' ');

List<int> numbers = new List<int>();
int b;

foreach (string digit in splittedText)
{
    if (int.TryParse(digit, out b))
        numbers.Add(b);
}

int[] numbersArray = numbers.ToArray();


Hope this helps.
 
Share this answer
 
v4
Comments
Mohammad A Rahman 20-Jul-11 4:47am    
My 5.
Good One :)
Sufi Saint 20-Jul-11 7:03am    
Hey Mario
The thing is that i ve a txt file havin some random digits seperated by empty space and i need to store each of the digit with a seperate index in the array
Mario Majčica 20-Jul-11 7:05am    
What kind of digits? Integers, decimals, ...?
Do you have an exemple of the txt file?
Sufi Saint 20-Jul-11 8:03am    
Its just a text file containing integers.
Mario Majčica 20-Jul-11 8:04am    
Then the edited solution will work. If so, accept the solution and vote! ;)
It depends on the size of your file and how much effort you want to put. If the files aren't large you can use Stream Reader's ReadToEnd[^] to read the entire contents to a string, then use String.Split[^] to fulfill your task.

If the files are large you need to buffer getting the data from the StreamReader, to conserve memory:

  1. If you are actually reading data line-by-line (rather than parsing text for example) you can use StreamReader.ReadLine to process each record individually, this reduces the memory hit.
  2. Read a set of bytes at a time (using the ReadBlock method for example) getting the results into a string and splitting them. One drawback of this method is that it is likely that the last item item will be split across two buffer reads, so you will need to keep the last item of block n the last character is not a space and append the first item of block n+1 if its first character is not
  3. You can just read a byte/char at a time, but this is much slower than option 2IIRC. If you use this method you can just build a string for the current word and add it to the array once you hit the next space.

Hope this helps
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jul-11 3:56am    
Complete, my 5.
--SA
Try
C#
char[] sep = new char[] { ' ' };
myArray = myText.Split(sep);
 
Share this answer
 
Comments
Keith Barrow 20-Jul-11 5:20am    
I think the difficulty might be getting the text from the file :). Not my 1-vote BTW.
Abhinav S 21-Jul-11 4:04am    
Point noted. And I thought the OP already had that.
I hope it helps,

C#
string fileName = @"c:\temp\test.txt";
var result = File.ReadLines(fileName).Select(line => line.Split(new char[] { ' ' }));


:)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jul-11 4:00am    
Limited applicability (small files; see the solution by Keith Barrow), but elegant use of Select. My 5. Disagree with the vote of 1.
--SA
Mohammad A Rahman 21-Jul-11 4:06am    
Thank you SA. :)
string line;
string[] splittedLine;
// Read the file and display it line by line.
System.IO.StreamReader file =
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
  splittedLine = line.Split(' ');
  // now you have an array of string[]
}

file.Close();
 
Share this answer
 
v2

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