You can use StringBuffer
or StringBuilder
for this task, StringBuilder
would be my choice since its more efficient. its not thread safe so multiple threads can call its methods simultaneously.
String reversedString = new StringBuilder(originalString).reverse().toString()
If you prefer not to use API support you can do something like this
static String reverse(String stringIn) {
char[] cArr = stringIn.toCharArray();
for (int i = 0; i < cArr.length/2; ++i){
char c = cArr[i];
cArr[i] = cArr[cArr.length-1-i];
cArr[cArr.length-1-i] = c;
}
return new String(cArr);
}
2
solved How to invert a word? [closed]