Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Different ways to reverse String in java - Testingpool

Different ways to reverse String in java

There can be different ways to reverse the String. Here , we are mentioning few.

Let’s understand them with example.

1. We can reverse the string by using a for loop and method charAt.

		String OriginalString = "Hello World!!";
		String temp = "";
		System.out.println("Original String is "+OriginalString);
		
		for(int i=0;i<OriginalString.length();i++){
			char ch = OriginalString.charAt(i);
			temp  = ch +temp;
		}

		System.out.println("Reversed string is "+temp);
Output:
Original String is Hello World!!
Reversed string is !!dlroW olleH

 

2. Using the StringBuffer class.

public class StringReverse {

	public static void main(String[] args) throws InterruptedException {
		
		String OriginalString = "Hello World!!";
		String temp = "";
		System.out.println("Original String is "+OriginalString);
		StringBuffer strBfr = new StringBuffer(OriginalString);
		strBfr.reverse();
		System.out.println("Reversed string is "+strBfr);
	}

}
Output:
Original String is Hello World!!
Reversed string is !!dlroW olleH

 

3. Using the StringBuilder class.

public class StringReverse {

	public static void main(String[] args) throws InterruptedException {
		
		String OriginalString = "Hello World!!";
		String temp = "";
		System.out.println("Original String is "+OriginalString);
		StringBuilder strBld = new StringBuilder(OriginalString);
		strBld.reverse();
		System.out.println("Reversed string is "+strBld);
	}

}
Output:
Original String is Hello World!!
Reversed string is !!dlroW olleH
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...