How to print each character from a string?

前端 未结 3 1710
离开以前
离开以前 2021-01-15 06:55

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

相关标签:
3条回答
  • 2021-01-15 07:00

    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);
    }
    
    0 讨论(0)
  • 2021-01-15 07:16

    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))));
    
    0 讨论(0)
  • 2021-01-15 07:18

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

    0 讨论(0)
提交回复
热议问题