Problems with split
Hello guys,
I have to read information from a text file line by line and then split the line into "fields".
The separator delimeter in "\n".
I'm using this code:
string [] sTheResult;
char[] delimeter = {'\n'};
int iFields = 0;
using(StreamReader rFile = new StreamReader(myFile))
{
sLine = (string)rFile.ReadLine();
while(sLine != null)
{
sTheResult = sLine.Split(delimeter);
foreach(string s in sTheResult)
{
sFields[iFields] = s;
iFields++;
}
sLine = (string)rFile.ReadLine();
}
}
What sLine contains is:
@"ONE\nTWO\nTHREE\nFOUR\nFIVE\nSIX\nSEVEN\nEIGHT\nNINE\nTEN\nELEVEN"
The problem is split is not working, because when I executje the "foreach" statement, "s" still contains the value of sLine !
Does anybody know what am i doing wrong ?
Thank you all
[953 byte] By [
Michael] at [2007-11-11 10:08:12]

# 1 Re: Problems with split
First let me say I don't program in C# so take what I say with a grain of salt :D
You example declares a char as {'\n'}
Are those supposed to be single quotes?
\n is a new line character, not "\n" as in your example search string. If read from the file each value should be on a seperate line. If the string "\n" is the delimiter as in your example then this should work (VB):
Dim strTest As String = "ONE\nTWO\nTHREE\nFOUR\nFIVE\nSIX\nSEVEN\nEIGHT\nNINE\nTEN\nELEVEN"
Dim str() As String
Dim strResult As String = ""
Dim strDelim As String = "\n"
str = Split(strTest, strDelim)
For Each strResult In str
Debug.Print(strResult)
Next
# 2 Re: Problems with split
Michael,
Is this what you need?
string s = "Test1\nTest2\nTest3\nTest4\nTest5\nTest6\nTest7\nTest8\nTest9\nTest10";
string[] Result = new string[11];
Result = s.Split('\n');
// Just for debug purposes::
for (int x = 0; x < Result.Length; ++x) Console.WriteLine(Result[x]);
Console.ReadKey();
You read in a line of text with fields delimited by newlines ('\n') and place each field in an array index. This seems to be the most efficient way I can think of doing it.. You can just change the code to create a dynamic size array.
# 3 Re: Problems with split
First of all I want to thank both of you for helping.
The thing is split works well when you initialize a string variable with this value :
"Test1\nTest2\nTest3\nTest4\nTest5\nTest6\nTest7\nTest8\nTest9\nTest10";
I mean when you use this code:
string s = "Test1\nTest2\nTest3\nTest4\nTest5\nTest6\nTest7\nTest8\nTest9\nTest10";
But if you write this string:
Test1\nTest2\nTest3\nTest4\nTest5\nTest6\nTest7\nTest8\nTest9\nTest10
Into a text file and then read it using the StreamReader and assign the result of the ReadLine() method to a string variable like this:
sLine = (string)rFile.ReadLine();
Split doesn't work and I don't know why... :confused:
# 4 Re: Problems with split
Michael,
The I think split function is only used for Chars =( When you read in the string delimited by \n's and try to use Split on it, it only removes the \ , which is why it wasn't working.
Is there any way you can change the way you're delimiting the fields? Can you use a ";" ?
Test1;Test2;Test3;Test4;Test5;Test6;Test7;Test8;Test9;Test10 ?
If you could change it to use a single char for delimiting, split would work.. In any case, if you're unable to change it from \n let me know, I can probably write something for the other case
# 5 Re: Problems with split
Hello kryologik,
In fact I thought the same thing (replacing the \n with another character), The thing is the text file is the resulting data delivered by a web service, and I can't change it in the source because it was developer by third patry developer.
I will use replace \n with any other character once I have the data loaded in the string variable.
Thank you very much for helping... :WAVE: