Escape variables in java

Take an example:

System.out.println("Hello  Uni\u03C0");
System.out.println("Hello Esc \\");

it gives something like

Hi Uniπ

Hi Esc \

Is there a way where I can give different values ​​for 03C0 and \ during different iterations in a loop?

for example something like

System.out.format("Hello Esc \%c",'\\');
System.out.format("Hello Esc \%c",'\"');

I know this will give a compiler error. I want to know how to do this.

For example, I would like to print a different Unicode character (e.g., from \ u0000 to \ u00C3) at each iteration of the loop.

For example, I have this function that returns the four-digit hexadecimal value of an integer:

public static String hexa(int a)
    {
    int x=a;
    String b= String.format("%x",x);
    if(b.length() == 1)
    {
    b="000"+b;
    }
    if(b.length() == 2)
    {
    b="00"+b;
    }
     if(b.length() == 3)
    {
    b="0"+b;
    }
    return b;
    }

Now I would like to join \ u with hexa (i) to get a different Unicode character for different i

+4
source share
1 answer

. Character.toChars()

    StringBuilder sb = new StringBuilder();
    sb.append(Character.toChars(0x03C0));
    System.out.println(sb.toString());

for:

public static void main(String [] args) {
    String lineSeparator = System.lineSeparator();
    StringBuilder sb = new StringBuilder();

    for(int i = 0x03C0; i < 0x03D0; i++) {
        sb.append(Character.toChars(i)).append(lineSeparator);
    }

    System.out.println(sb.toString());
}

:

π
ρ
ς
σ
τ
υ
φ
χ
ψ
ω
ϊ
ϋ
ό
ύ
ώ
Ϗ

:

System.out.println(Character.toChars(0x03C0)[0] == '\u03C0');

:

true

StringBuilder:

String foo = "";
for(char c : Character.toChars(0x03C0)) {
    foo += c;
}
System.out.println(foo);
+3

All Articles