Java經典例項:處理單個字串

FrankYou發表於2016-11-08

使用for迴圈和String物件的charAt()方法;或者,使用“for each”迴圈和String物件的toCharArray()方法。

/**
 * Created by Frank
 */
public class StrCharAt {
    public static void main(String[] args) {
        String a = "A quick bronze fox lept a lazy bovine";
        for (int i = 0; i < a.length(); i++) {
            System.out.println("Char " + i + " is " + a.charAt(i));
        }
    }
}
public class ForEachChar {
    public static void main(String[] args) {
        String s = "Hello Wold";
        for (char c : s.toCharArray()) {
            System.out.println(c);
        }
    }
}

 

相關文章