One of the most common operations on sequences is to iterate over its elements. This way, we can do multiple operations on each of its elements.
In this section, we can use the for statement with a combination of Python range(), slicing operator and other few functions and methods for this purpose.
xxxxxxxxxx
const iterable = 'boo';
for (const value of iterable) {
console.log(value);
}
// "b"
// "o"
// "o"
xxxxxxxxxx
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
class Main
{
// Iterate over the characters of a string
public static void main(String[] args)
{
String s = "Techie Delight";
CharacterIterator it = new StringCharacterIterator(s);
while (it.current() != CharacterIterator.DONE)
{
System.out.print(it.current());
it.next();
}
}
}