user2391084
user2391084

Reputation:

How to print each character from a string?

Is there a way to read a single character at a time from the input and process it, without tokenizing the vocabulary?

Upvotes: 2

Views: 6210

Answers (3)

The_301
The_301

Reputation: 851

The toCharArray() function on Strings might be useful here.

for(char c : s.toCharArray())
     System.out.println(c);

And to print only the lowercase ones in the string- Thanks @fge

for(Character c : s.toCharArray()){
        if(Character.isLowerCase(c))
        System.out.println(c);
}

Upvotes: 12

dogbane
dogbane

Reputation: 274622

You can use Guava's CharMatcher to extract lower case letters from your string, then use Joiner to put each character on a new line.

You can do all this in a single line of code as shown below:

System.out.println(Joiner.on('\n').join(Lists.charactersOf(CharMatcher.JAVA_LOWER_CASE.retainFrom(s))));

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117597

You can use read() from BufferedReader which reads one char at a time.

Upvotes: 3

Related Questions