4

After overcoming the issues in printing non-ascii characters described here, I wonder if is it possible to configure the console to print Right-To-Left ?

I am now playing with Visual Studio T4 templating, wanted to see for my self the outcome of trying to automatically generate Enums out of database look up tables - and for that I wanted to test myself in this console application I am writing for this purpose.

Obviously, as a workaround, printing the following helps me:

new string(str.ToCharArray().Reverse().ToArray())

But does the console support RTL printing ?

Community
  • 1
  • 1
Veverke
  • 9,208
  • 4
  • 51
  • 95
  • I don't think the console supports RTL, as per [point 3 here](https://msdn.microsoft.com/en-us/library/a9d2b0zt.aspx): _"Console applications. Console applications do not include text support for bi-directional languages. This is a consequence of how Windows works with console applications."_ Left as a comment, not an answer, as I don't know for sure or if there are workarounds etc. – James Thorpe Sep 21 '15 at 14:18
  • @JamesThorpe: thanks James. – Veverke Sep 21 '15 at 14:19
  • @JamesThorpe: Hey James, I added a workaround. – Veverke Jul 28 '16 at 14:54

2 Answers2

3

If you also want to print your strings aligned to the right side of the screen in addition to have it rendered in a reversed manner (RTL), use this:

private static void WriteLineRtl(string input)
{              
  Console.CursorLeft = Console.WindowWidth - input.Length;
  for (int i = input.Length - 1; i >= 0; i--)        
      Console.Write(input[i]); 
}

Example:

using static System.Console;

class Program
{
  static void Main(string[] args)
  {
    WriteLineRtl("שלום,");
    WriteLineRtl("בהמשך לשיחתנו הטלפונית,");
    WriteLineRtl("רציתי להודיע שהכל בסדר.");
    WriteLineRtl("שלום ולהתראות!");

    Read();
  }

  static void WriteLineRtl(string input)
  {
    CursorLeft = WindowWidth - input.Length;
    for (int i = input.Length - 1; i >= 0; i--)
      Write(input[i]);
  }
}

Output:

enter image description here

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
2

Having found no built-in support, I learned from other posts that you can play with the Console's cursor. So something like the following will do it:

private static void Main(string[] args)
{
    Console.OutputEncoding = Encoding.Unicode;

    List<string> sentences = new List<string>();

    sentences.Add(string.Format("אברהם"));
    sentences.Add(string.Format("שורה {0}, אחרי אברהם", 2));

    foreach (var sentence in sentences)
    {
        WriteLineRTL(sentence);
    }
}

private static void WriteLineRTL(string input)
{
    var chars = input.ToCharArray();
    Console.CursorLeft = chars.Length;
    for (int i = 0; i < chars.Length; i++)
    {
        Console.Write(chars[i]);
        Console.CursorLeft -= 2;
    }
    Console.WriteLine();
}

Output:

enter image description here

Veverke
  • 9,208
  • 4
  • 51
  • 95
  • instead of doing `chars.ElementAt(i)` just do `chars[i]`, ElementAt is for when you are working with a `IEnumerable` and you don't have a indexer available. – Scott Chamberlain Jul 28 '16 at 14:09