TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Reverse String

Reverse the characters in a string using a char array and the charAt method.
Reverse string. Imagine a string contains a sequence of characters. To create another version of that string, we can reverse it. This can be used to form a string key.Strings
CharAt, String constructor. We use many parts from Java to reverse a string. We create a mutable char array buffer. We use charAt to get (in inverse order) chars from the original string.

And: We use the String constructor to create a string from the char buffer we filled in the loop.

Constructors
Method example. We introduce the reverseString method. First we allocate a char array with a size equal to the string's length. Then we loop the array elements.

And: We assign the array elements to a string character indexed from the opposite (inverted) side. This reverses the characters.

Char Arrays

Finally: We create a string and return it. In main we find that the reverseString works as expected on simple strings.

Java program that reverses Strings public class Program { public static String reverseString(String value) { // Create a char buffer. char[] array = new char[value.length()]; for (int i = 0; i < array.length; i++) { // Assign to the array from the string in reverse order. // ... charAt uses an index from the last. array[i] = value.charAt(value.length() - i - 1); } return new String(array); } public static void main(String[] args) { // Test our reverse string method. String original = "abc"; String reversed = reverseString(original); System.out.println(original); System.out.println(reversed); original = "qwerty"; reversed = reverseString(original); System.out.println(original); System.out.println(reversed); } } Output abc cba qwerty ytrewq
A discussion. The Arrays class has no reverse method. This means that we must reverse these characters manually. We find a reverse() on Collections.

But: Collections.reverse would require the string be converted into a collection like an ArrayList. This would hinder performance.

ArrayList
Offset. The tricky part with the reverseString method is the indexing expression into the string. This is the argument to charAt. We use the index and count from the last index.
StringBuilder. Here is an alternative method. StringBuilder has a reverse method. We can create a StringBuilder from a string and then reverse it.StringBuilder
To conclude, string reversal is not a common requirement. But for certain problems (like key generation) or for homework assignments, it has use.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf