java string invert code example
Example 1: Reverse a string in java
class ReverseUsingReverseMethod
{
public static void main(String[] args)
{
String str = "Hello world Java";
StringBuilder sb = new StringBuilder();
sb.append(str);
sb = sb.reverse();
System.out.println(sb);
}
}
Example 2: Reverse a string in java
class ReverseStringByteArray
{
public static void main(String[] args)
{
String input = "HelloWorld";
byte[] strByteArray = input.getBytes();
byte[] output = new byte[strByteArray.length];
for(int a = 0; a < strByteArray.length; a++)
output[a] = strByteArray[strByteArray.length - a - 1];
System.out.println(new String(output));
}
}