/*取得字串的長度和其中的字元*/
class Sample1
{
public static void main(String[] args)
{
String str="Hello";
char ch1=str.charAt(0); //取得字串的第一個字元
char ch2=str.charAt(1); //取得字串的第二個字元
int len=str.length(); //傳回字串的長度
System.out.println(str + "的第一個字元是" + ch1 + "。");
System.out.println(str + "的第二個字元是" + ch2 + "。");
System.out.println(str + "的長度是" + len + "。");
}
}
沒有留言:
張貼留言