所以 TextReader.ReadLine() 返回一个字符串,而 TextReader.Read() 返回一个 int 值。这个 int 值似乎也是某种我不认识的格式。是否可以将此整数转换为字符?感谢您的帮助。
编辑:
TextReader 值 = new StreamReader(@"txt");
string SValue1;
int Value1;
Value1 = Values.Read();
Console.WriteLine(Value1);
Console.ReadKey();
当它读出值时,输出为 51。 txt文件的第一个字符是3,为什么会这样?
最佳答案
根据 the documentation for the StringReader class (TextReader的一个子类),Read()
的返回值可以转换为char,但是需要先检查是否在文件/字符串的末尾(通过检查对于 -1)。例如,在文档中修改后的代码中:
while (true)
{
int integer = stringReader.Read();
// Check for the end of the string before converting to a character.
if (integer == -1)
break;
char character = (char) integer; // CONVERT TO CHAR HERE
// do stuff with character...
}
关于C#。为什么当我使用 TextReader.Read() 时它返回一个 int 值?可以转换为char吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6487721/