HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

Reverse a string in Java

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
javastringreverse

Problem

I have "Hello World" kept in a String variable named hi.

I need to print it, but reversed.

How can I do this? I understand there is some kind of a function already built-in into Java that does that.

Related: Reverse each individual word of “Hello World” string with Java

Solution

You can use this:
new StringBuilder(hi).reverse().toString()


StringBuilder was added in Java 5. For versions prior to Java 5, the StringBuffer class can be used instead — it has the same API.

Context

Stack Overflow Q#7569335, score: 1224

Revisions (0)

No revisions yet.