G
Guest
Dear all,
I use streamreader to read a file line by line. However, both UTF8 and
Unicode exists in each line. Is there a way to convert a substring's encoding of a string there is came from streamreader? Here is my code:
private String searchChapter(string book, string chap, string filePath)
{
String Chapter = "";
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filePath))
{
// Read and display lines from the file until the end of
// the file is reached.
int colonIdx;
String line;
bool isFound = false;
//testing
Chapter = sr.CurrentEncoding.ToString() + "\r\n\r\n";
while ((line = sr.ReadLine()) != null)
{
//Console.WriteLine(line);
colonIdx = line.IndexOf(":");
if (line.Substring(0,3) == book && line.Substring(4,colonIdx - 4) == chap)
{
isFound = true;
}
Chapter = Chapter + line + "\r\n\r\n";
}
else if (isFound == true) break;
}
}
return Chapter;
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
return Chapter;
}
I use streamreader to read a file line by line. However, both UTF8 and
Unicode exists in each line. Is there a way to convert a substring's encoding of a string there is came from streamreader? Here is my code:
private String searchChapter(string book, string chap, string filePath)
{
String Chapter = "";
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filePath))
{
// Read and display lines from the file until the end of
// the file is reached.
int colonIdx;
String line;
bool isFound = false;
//testing
Chapter = sr.CurrentEncoding.ToString() + "\r\n\r\n";
while ((line = sr.ReadLine()) != null)
{
//Console.WriteLine(line);
colonIdx = line.IndexOf(":");
if (line.Substring(0,3) == book && line.Substring(4,colonIdx - 4) == chap)
{
isFound = true;
}
Chapter = Chapter + line + "\r\n\r\n";
}
else if (isFound == true) break;
}
}
return Chapter;
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
return Chapter;
}