Languages
[Edit]
EN

Java - iterate string backwards

0 points
Created by:
Wallace
636

In this article, we would like to show you how to iterate string backwards in Java.

Quick solution:

 

1. Practical example using for loop

Edit

In this example, we use simple for loop to iterate the text string backwards.

Output:

2. Using String split() method

Edit

In this example, we use String split() method with empty string ("") as a separator to split the text string into array of strings (letters). Then we iterate through the letters array using simple for loop.

Output:

3. Converting string to character array

Edit

In this example, we create new StringBuilder object from the text string, reverse it and convert to characters array. Then we simply iterate over the characters array using enhanced for loop.

Output:

4. Using iterator

Edit

 In this example, we use the CharacterIterator with its last() and previous() methods to iterate the text string backwards.

Output:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join