Java基础系列——String的方法(21)
Stirng类的方法有很多,本片博客描述的就是所有的方法,包括一些新的方法(主要是JDK1.8之后新出的方法)。 charAt( int index ) charAt方法描述: Returns the char value at the specified index.返回指定位置的字符 那么也就是说,在方法中传入一个参数,返回一个具体的位置。具体代码如下: public class TestString5 { public static void main(String[] args) { String s = "http://oschina.net/lujiapeng" ; char c = s.charAt( 3 ) ; System.out.println( c ); // p c = s.charAt( 9 ) ; System.out.println( c ); // c c = s.charAt( 100 ) ; System.out.println( c ); // StringIndexOutOfBoundsException: String index out of range: 100 } } 那么在这里清楚的看到,当传入一个3的时候,会返回一个字符p,如果传入一个9的话,会返回一个字符c,如果传入100的话,会抛出一个异常