Home > Java > javaTutorial > body text

In Java, what is the easiest way to reverse a string?

WBOY
Release: 2023-09-07 18:13:02
forward
776 people have browsed it

In Java, what is the easiest way to reverse a string?

Built-in reverse() method

The StringBuffer class provides you with a method called reverse(). It inverts the contents of the current StringBuffer object and returns the resulting StringBuffer object. This is the easiest way to reverse a Sting using Java. To do this -

  • # Instantiate the StringBuffer class by passing the required String as parameter.

  • Call the reverse() method on the created object.

  • Convert it to String again using toString() method.

Example

public class Sample {
   public static void main(String args[]) {
      String str = new String("Hello how are you");
      StringBuffer sb = new StringBuffer(str);
      String str2 = sb.reverse().toString();
      System.out.println(str2);
   }
}
Copy after login

Output

uoy era woh olleH
Copy after login

Let us observe two other ways to reverse a string

Using recursion

Recursion is a process of calling a function within itself. The following java program uses recursion to reverse Sting -< /p>

Example

public class StringReverse {
   public String reverseString(String str) {
      if(str.isEmpty()) {
         return str;
      }else {
         return reverseString(str.substring(1))+str.charAt(0);
      }
   }
   public static void main(String[] args) {
      StringReverse obj = new StringReverse();
      String result = obj.reverseString("Tutorialspoint");
      System.out.println(result);
   }
}
Copy after login

Output

tniopslairotuT
Copy after login

Use toCharArray( )

You can also convert a string to a character array and swap the characters of the array.

To reverse an array, place the first element with the last element with the second element with the second to last element, and so on, leaving the middle elements unchanged if the array length is odd.

If i is the first element of the array (length array –i-1) the position of will be the last element, therefore, compare array[i] with array[ (array –i-1)] Swap the midpoint of the array from beginning to end -

Example

import java.util.Arrays;
public class StringReverse {
   public static void main(String[] args) {
      String str = "Tutorialspoint";
      char[] myArray = str.toCharArray();
      int size = myArray.length;
      for (int i = 0; i < size / 2; i++) {
         char temp = myArray[i];
         myArray[i] = myArray[size - 1 - i];
         myArray[size - 1 - i] = temp;
      }
      System.out.println("Array after reverse:: ");
      System.out.println(Arrays.toString(myArray));
   }
}
Copy after login

Output

Array after reverse::
[t, n, i, o, p, s, l, a, i, r, o, t, u, T]
Copy after login

The above is the detailed content of In Java, what is the easiest way to reverse a string?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!