Search code examples
javaright-to-leftdirection

How to change java string direction from ltr to rtl?


I'm trying to change direction from ltr to rtl to the string using java programming language . My string

String newString = "\nالفرع: الفرع الرئيسي\n" +
            "***********************************\n" +
            "التاريخ والوقت: 2019/01/0218:01\n" +
            "نوع الحركة: مبيعات مضخات  \n" +
            "رقم الفاتورة: 14\n" +
            "طريقة الدفع: نقد  \n" +
            "المضخة: م 1 ك 1\n" +
            "\n" +
            "\n" +
            "نوع الوقود: بنزين 95\n" +
            "الكمية: 2.830\n" +
            "السعر: 1.060\n" +
            "الإجمالي: 3.000\n" +
            "\n" +
            "***********************************\n" +
            "          شكرا لزيارتكم\n" +
            " \n" +
            " ";

Solution

  • You're trying to change the format, which requires change in the string. Use String.format with a given width which will add the necessary spaces to align your string to the right or left as you need:

    static String align(String str, int width, boolean right){
        String[] temp = str.split("\n");
        String frmt = "";
        if(right){
            frmt = "%" + width + "s";
        }
        else{
            frmt = "%-" + width + "s";
        }
    
        for(int i=0; i<temp.length; i++)
            temp[i] = String.format(frmt, temp[i]);
        return String.join("\n", temp);
    }
    
    public static void main(String[] args) {
        String newString = "\nالفرع: الفرع الرئيسي\n" +
                    "***********************************\n" +
                    "التاريخ والوقت: 2019/01/0218:01\n" +
                    "نوع الحركة: مبيعات مضخات  \n" +
                    "رقم الفاتورة: 14\n" +
                    "طريقة الدفع: نقد  \n" +
                    "المضخة: م 1 ك 1\n" +
                    "\n" +
                    "\n" +
                    "نوع الوقود: بنزين 95\n" +
                    "الكمية: 2.830\n" +
                    "السعر: 1.060\n" +
                    "الإجمالي: 3.000\n" +
                    "\n" +
                    "***********************************\n" +
                    "          شكرا لزيارتكم\n" +
                    " \n" +
                    " ";
        System.out.println(align(newString, 35, false));
    
    }
    

    Width here is set to 35, you can set it to whatever you need. ̶T̶h̶e̶ ̶̶-̶̶ ̶w̶i̶l̶l̶ ̶a̶l̶i̶g̶n̶ ̶i̶t̶ ̶t̶o̶ ̶t̶h̶e̶ ̶r̶i̶g̶h̶t̶.̶ ̶


    Edit: I changed it further, the function is now called align and accepts a new parameter right which tells it whether to align to right or left as can be seen when changing the frmt variable.


    Edit 2: the - sign will align to left if the language is ltr and it will align to right if the language is rtl. Arabic (shown in your example) is rtl and therefore, you need to align in opposite to what you have in English. I have updated the function with the corrected code. For RTL languages (Arabic), use align(string, width, false) and for LTR languages use align(string, with, true)