Assume we want to remove the last unwanted ,
in the following code.
for (int i = 0; i < 5; i++)
{
foreach (var a in "Money")
Console.Write($"{a},");
}
If we only add Console.Write("\b\n");
as follows,
for (int i = 0; i < 5; i++)
{
foreach (var a in "Money")
Console.Write($"{a},");
Console.Write("\b\n");
}
The output will be still the same. The unwanted trailing comma still exists.
But if we add \0
as follows,
for (int i = 0; i < 5; i++)
{
foreach (var a in "Money")
Console.Write($"{a},");
Console.Write("\b\0\n");
}
The unwanted trailing ,
vanishes.
Someone said that using \0
is just a hack that might break in the future updates.
So, what is the correct and safe way to remove the trailing comma if I insist on using \b\n
sequence?
Edit
Not only on the terminal output, \b
in the following does not remove the previous character.
using (StreamWriter sw = new StreamWriter("output.txt"))
{
for (int i = 0; i < 5; i++)
{
foreach (var a in "Money")
sw.Write($"{a},");
sw.Write("\b\n");
}
}