Split function

G

Guest

I have the following code. bit it gives me error that The best overload
method for string.Split(params char[]) has some invalid arguments. I am
using stringbuilder which doesn't have split method so how can I do that.


public static string[] ReadFileLines(string path)
{
int read = 0;
StringBuilder builder = new StringBuilder();
byte[] buffer = new byte[1024];

using (FileStream readStream = File.Open(path, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
while ((read = readStream.Read(buffer, 0, buffer.Length)) > 0)
builder.Append(Encoding.UTF8.GetString(buffer, 0, read));
}


return String.Split(builder.ToString(), "\r\n");

}
 
A

Alberto Poblacion

bobby said:
I have the following code. bit it gives me error that The best overload
method for string.Split(params char[]) has some invalid arguments.
[...]
return String.Split(builder.ToString(), "\r\n");

Do the Split on the result of converting the StringBuilder to String, and
then pass several chars to satisfy the "params char[]" argument:

return builder.ToString().Split('\r', '\n');
 
A

Alberto Poblacion

In said:
return builder.ToString().Split('\r', '\n');

Now that I think about it, this is probably not what you intended in the
first place, since this will break the string on every \n and every \r, and
I suspect that you want to split on the "\r\n" sequence. This can be done
with a new overload of Split available in the Framework 2:

Split("\r\n", StringSplitOptions.None);
 
G

Guest

bobby said:
I have the following code. bit it gives me error that The best overload
method for string.Split(params char[]) has some invalid arguments.
return String.Split(builder.ToString(), "\r\n");

return builder.ToString().Split("\r\n".ToCharArray());

maybe.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top