7

I am converting C# code to Java, and I came across this line (where i is an int).

sb.AppendFormat("\\u{0:X04}", i);

From what I can see, Java does not have an appendFormat method on its StringBuilder.

How would I go about converting this?

EDIT:

I see that AppendFormat is just a combination of append and String.format. How would I convert \\u{0:X04} to Java's String.format?

1
  • 1
    Not sure why Java never seems to add these obviously helpful methods. I'd expect there to be an append(string format, Object.. args) overload (or perhaps named appendFormat), but there's not. Commented Jun 21, 2018 at 19:00

5 Answers 5

6

The java.util.Formatter class has a zero-argument constructor which automatically wraps a StringBuilder:

Formatter formatter = new Formatter();
formatter.format("\\u%04x", i);

// ...

String finalText = formatter.toString();

// Or, if you want to be explicit about it:
//StringBuilder sb = (StringBuilder) formatter.out();
//String finalText = sb.toString();
Sign up to request clarification or add additional context in comments.

3 Comments

Formatter implements Closeable, don't forget to close it
@Laymain new Formatter() is equivalent to new Formatter(new StringBuilder()). Closing a Formatter only has meaning if the underlying Appendable implements Closeable.
@VCR Good point indeed, however it's still a good pratice to close Closeable objects, if implementation changes or if a Closeable implementation is passed to the constructor, developer won't forget to close it.
3

String.format() may do the job for you. So in turn say:

sb.append(String.format("\\u{0:X04}", i));

2 Comments

Ah right, would you know how to convert \\u{0:X04} to javas String.format?
I have no idea of anything in c#, sorry.
3

Most of the answers contains little errors (two 0 instead of one, C# format leaved in Java code).

Here is working snipped:

sb.append(String.format("\\u%04X", 0xfcc));

Regarding comments: C# specifier \\u{0:X04} and Java specifier \\u%04X both produce numbers in format \uXXXX where X are upper case hex digits (when you use small x you will get lower case hex digits), so it matters.

Comments

1

You need a combination of 'append' and 'String.format', remembering to adjust the format specifier for Java:

sb.append(String.format("\\u%004X", i));

2 Comments

Comparing your answer to VGR's...Two 0's or just one? Does the the capitalization of the x matter?
Can't check that right now - try both to see which works. The main difference with my answer is that you just need one line for the Java equivalent.
0

Other answers haw a flow that intermediate string is being created. Except accepted answer, but it asumes that we don't already have StringBuilder. And yes we can use Formatter INSTEAD of SB. But in many cases in code we receive SB from outside, so we can wrap it into formatter.

Correct answer that have full correspondence to provided C# code is:

// sb.AppendFormat("\\u{0:X04}", i);
Formatter fmt = new Formatter(sb);
fmt.format("\\u%04x", i);

Data would be appended to end of string builder directly. No intermediate string is created. You can also specify locale if needed.

Later applies if we feed to Formatter not String builder, but arbitrary Appendable, like text file.

We don't need to mange lifetime of formatter directly( close it), if we would manage lifetime of writer (if that's not SB but file). If we pass in Writter and don't use that variable anymore -- we would need to close() Fromatter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.