Java Example – String reversal
The following example demonstrates how to reverse a string with Java's reverse() function:
StringReverseExample.java file
public
class
StringReverseExample
{
public
static
void
main
(
String
[
]
args
)
{
String
string
=
"
runoob
"
;
String
reverse
=
new
StringBuffer
(
string
)
.
reverse
(
)
.
toString
(
)
;
System
.
out
.
println
(
"
Before reversing the string:
"
string
)
;
System
.
out
.
println
(
"
After reversing the string:
"
reverse
)
;
}
}
The output of the above code example is:
字符串反转前:runoob
字符串反转后:boonur
Java Examples