I am working on a school project and the task was to create an application, which one of the features is to save multi-lined text (user input) into a string. I know some parts of the code might be outdated, but our school compiler only supports .NET version 4 (edit: I compile in Visual Studio 2022, school compiler is only the place where we showcase the final work, that is why the version is a limit for finished work). I am still learning and I am sure there are other things that could be improved, but let’s look at the main problem.
When the user types standard Czech text (for example "včera", which means yesterday), the text saved and later returned shows "vera". If I type only these special characters, for example "ůčšěž", which doesn’t mean anything, the string content is left empty.
When I create a string using different method, for example basic Console.WriteLine()
, the special characters are saved just okay.
The method I am using is supposed to take user input and supply that multi-lined text together with DateTime to an object Record inside a doubly linked list. Below, I will show the relevant code because the mistake could be created on the way.
public static void CreateNewRecord()
{
Console.Write("Date: ");
if (DateTime.TryParse(Console.ReadLine(), out DateTime parsedDate))
{
Console.WriteLine("nText: ");
StringBuilder sb = new StringBuilder();
while (true)
{
string textLine = Console.ReadLine();
if (textLine.ToLower() == "save")
break;
sb.AppendLine(textLine);
}
string recordText = sb.ToString();
Program.records.AddLast(new Record(parsedDate, recordText));
}
else
{
Console.WriteLine("Wrong date format.");
}
}
This creates the Record and and using the overriden ToString(), I am trying to read it:
public override string ToString()
{
return string.Format("Date: {0}nn{1}", Date, Text);
}
However, when trying to read the content using Console.WriteLine(records.Last());
, only english-lettered text is returned.
If anyone has idea what is wrong, your answer will be welcomed.
I tried to add UTF-8 encoding to the top of my program, which didn’t help. I tried to create a very simple string builder that appends parts of text (not whole lines), and it returned even with the special characters.
I expect a multi-lined text to be returned with all the symbols the user saves to it.
3
Answers
Thank you everyone for your time. I tried to run the code on different computer and the error does not show up there. Special characters are saved. It looks like I have my system console corrupted. I do not exactly know where the error is, but it is not error in the code, therefore not suitable for this forum. Thank again, everyone.
To help you handle special characters properly:
The issue you’re facing might be related to the encoding used when reading and writing the text. To ensure proper handling of special characters, you can try using the
Console.OutputEncoding
property to set the encoding to UTF-8 before writing the text. Here’s an example of how you can modify your code:By setting the
Console.OutputEncoding
toEncoding.UTF8
, you ensure that the console can handle and display the special characters properly.Hope this helps.