Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Examples – String Reversal

Java Example – String reversal

Java Examples Java Examples

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 Java Examples