public class StringCharacters { public static void main(String args[]){ String text = "To be or not to be, that is the question;"+ " Whether 'tis nobler in the mind to suffer"+ " the slings and arrows of outrageous fortune,"+ " or to take arms against a sea of troubles," + " and by opposing end them?"; int spaces = 0; int vowels = 0; int letters = 0; for(int i=0; i < text.length(); i++){ char ch = Character.toLowerCase(text.charAt(i)); if(ch == 'a' || ch =='e' || ch == 'i' || ch == 'o'|| ch =='u'){ vowels ++; } if(Character.isLetter(ch)){ letters ++; } if(Character.isSpace(ch)){ spaces ++; } } System.out.println("vowels is : " + vowels + "\nconstants is : " + (letters - vowels) + "\nspaces is : " + spaces); } }
2013年9月5日 星期四
String - charAt()
String 類別 (class) 有 charAt() 方法 (method)。
可取出String 物件裡的某個字元。
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言