public class TestString { public static char testCharAt(String str,int index){ if((index<0) || (index >= str.length())){ throw new StringIndexOutOfBoundsException(index); } char[] c1 = str.toCharArray(); System.out.println("字符串["+str+"]索引"+index+"爲:"+c1[index]); return c1[index]; } public static void main(String[] args) { TestString.testCharAt("abcd", 2); } } |