ALL >> Computer-Programming >> View Article
Different Ways To Reverse A String In Java
In this topic, we will learn about different ways to reverse a string in Java with examples.
different_ways_to_reverse_a_string_in_java
There are the following ways to reverse a String in Java:
• Using the String’s charAt() method
• Using the String’s toCharArray() method
• Using the String’s getBytes() method
• Using the StringBuilder’s reverse() method
• Using the StringBuffer’s reverse() method
Using the String’s charAt() method
We can reverse a String using the charAt() method of the String class. A String object is immutable and doesn’t have the in-built reverse() method. StringBuilder and StringBuffer classes have in-built reverse() methods.
Example
In this example, we will get each character of the String and add them to the font of the new String.
public class SpringJava{
public static void main (String[] args) {
String str= "springjava", nstr="";
char ch;
System.out.println("Original String: "+str);
for(int i=0; i= 0; i--)
System.out.print(charArr[i]);
...
... }
}
Output
avajgnirps
Using the String’s getBytes() method
We can reverse a String by using the getBytes() method in Java's String class.
Example
In this example, we will convert String to byte array to reverse String.
public class SpringJava {
public static void main(String[] args){
String input = "springjava";
//converting String to byte array
byte[] bytSArr = input.getBytes();
byte[] bytArr = new byte[bytSArr.length];
for (int i = 0; i < bytSArr.length; i++)
bytArr[i] = bytSArr[bytSArr.length - i - 1];
System.out.println(new String(bytArr));
}
}
Output
avajgnirps
Using the StringBuilder’s reverse() method
We can reverse a String by using the in-built reverse() method of the StringBuilder class in Java.
Example
In this example, we will take String and StringBuilder then append String to StringBuilder and apply the reverse() method of the StringBuilder to reverse String.
public class SpringJava {
public static void main(String[] args){
String input = "springjava";
StringBuilder sb = new StringBuilder();
//appending a string into StringBuilder
sb.append(input);
//reverse StringBuilder
sb.reverse();
//printing reversed String
System.out.println(sb);
}
}
Output
avajgnirps
Using the StringBuilder’s reverse() method
We can reverse String by using the in-built reverse() method of the StringBuffer class in Java.
Example
In this example, we will convert String to StringBuffer and then apply the reverse() method to reverse String in Java.
public class SpringJava {
public static void main(String[] args){
String str = "springjava";
//converting a String object to StringBuffer
StringBuffer sbr = new StringBuffer(str);
//to reverse that string
sbr.reverse();
System.out.println(sbr);
}
}
Output
avajgnirps
FAQ
Q1: How a string is reversed in Java?
Ans. The string class does have an in-built reverse() method but the StringBuffer class has a reverse() method. We can convert String to StringBuffer and then use the reverse() method to reverse a String in Java.
Q2: How do you reverse a word in Java?
Ans. Using the StringBuilder class's reverse() method, we can reverse a word in Java.
Conclusion
In this topic, we learned different ways to reverse String in Java
Add Comment
Computer Programming Articles
1. Which Institute Is Best For Coding And Programming In Bhopal?Author: Shankar Singh
2. Top 9 Benefits Of Custom Mobile Application Development
Author: Byteahead
3. Top 10 Creative Business Ideas For Entrepreneurs
Author: Byteahead
4. Top 10 Apps Like Tiktok Everyone Should Check Out
Author: Byteahead
5. Is The Apple Watch Series 7 Worth It For Seniors?
Author: Ashish
6. The Ultimate Guide To Ebay Product Listing Services: Elevate Your Online Store
Author: rachelvandereg
7. Which Are The Best Java Coding Classes In Bhopal?
Author: Shankar Singh
8. Warehouse Management In Zambia: Essential Features To Look For
Author: Doris Rose
9. Ecommerce Web Design And Development In Melbourne With The Merchant Buddy
Author: themerchantbuddy
10. Why Website Maintenance Is Crucial For Business Success
Author: Yogendra Shinde
11. Boost Your Business With Smart Invoice Pos Software In Zambia
Author: Cecilia Robert
12. How Stablecoin Development Ensures Stability And Security?
Author: Michael noah
13. Công Cụ Tính Chiều Cao Chuẩn Từ Minbin Tool: Đo Lường Và Cải Thiện Chiều Cao Hiệu Quả
Author: KenJi123
14. How To Make A Courier App For Courier Delivery And Tracking Service
Author: Deorwine Infotech
15. Reputation Management In The Digital Age: Protecting And Enhancing Your Law Firm’s Image
Author: jamewilliams