4

Take the example:

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

this gives something like

Hello Uniπ

Hello Esc \

Is there a way where I can give different values to 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 compiler error. I want to know how this can be done.

For example, I would like to print a different unicode character (say from \u0000 to \u00C3) in each iteration of a loop.

For example, I have this function that returns the 4 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 different unicode character for different i

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3015246
  • 139
  • 1
  • 2
  • 9

1 Answers1

3

You don't even have to convert the integer to hex string. Leave it as an integer and use Character.toChars()

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

Further example showing a for loop:

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());
}

Output:

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

One last clarification:

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

Output:

true

Example without StringBuilder:

String foo = "";
for(char c : Character.toChars(0x03C0)) {
    foo += c;
}
System.out.println(foo);
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Pace
  • 41,875
  • 13
  • 113
  • 156
  • I don't understand, how does that solve my problem? I want to say System.out.print("\u(VARIABLE)");How do I add the variable to \u or \ – user3015246 Nov 22 '13 at 18:54
  • You don't need the \u or the \. All those things do is convert source code to a character. If you run my example you'll see that it prints the pi character. I'll modify it to add a for loop. – Pace Nov 22 '13 at 20:05
  • **THANKS A LOT!** This is more or less what I needed. The only problem is I am not very familiar with StringBuilder class methods.What does append() do? and I guess I can declare an integer in decimal format with 0x as prefix? – user3015246 Nov 22 '13 at 20:10
  • That was just a convenience. I'll add an example without it. – Pace Nov 22 '13 at 20:10
  • I did it like this: for(i=20;i<20000;i++) { f=(char)(i); System.out.println( " \\u"+hexa(i) + " = "+ i+ " = "+f+"\n"); } – user3015246 Nov 22 '13 at 20:19
  • `StringBuilder` is like adding Strings with `+` but way more efficient, if you have multiple additions. It has to do with how Java internally implements the addition stuff. You first create an empty instance with `new StringBuilder()`, then you append your `String`s with `sb.append(first).append(second)` and so on. And in the end you get the final resulting `String` with `sb.toString()`. It is just to avoid using `first + second + ...` because, as said, it can be way more expensive. However in this answer I don't see a benefit in using it, it would even be a bit slower. – Zabuzard Aug 16 '17 at 05:16
  • However I think @Pace wanted to build **one** String with that Builder, instead of creating a new version every iteration. I think that is a **typo**, especially when looking at his non-builder variant. And if that was the goal, then the `StringBuilder` would be useful again. – Zabuzard Aug 16 '17 at 05:20
  • @Zabuza Even *I* don't know what Pace wanted to do 5 years ago. I'm not sure what the typo is but I'm happy to fix it. I believe I am creating one string. – Pace Aug 16 '17 at 15:44
  • Oh, that post is indeed pretty old... just realized that. I edited your post, take a look at the version before. There you created a single `StringBuilder` in each iteration. – Zabuzard Aug 16 '17 at 18:59
  • Had not realized you edited it, yes I agree with your edit entirely. Thanks! – Pace Aug 16 '17 at 21:53