字符串查找与截取

 1 public class helloworld
 2 {
 3     public static void main(String[] args) 
 4     {
 5         //截取
 6         String str="abcdef";
 7         System.out.println(str.substring(2,5));//从索引2开始,截取到索引5前一位的字符
 8         System.out.println(str.substring(2));//截取第二个索引后面的全部字符
 9         System.out.println(str.charAt(2));//截取在索引2上的字符
10         System.out.println(str.length());//截取字符串的长度
11         //查找
12         System.out.println(str.indexOf("cd"));//从左往右查找该字符在索引几上,
13         System.out.println(str.lastIndexOf("a"));//从右往左查找该字符在索引几上
14         System.out.println(str.startsWith("ab"));//判断字符串的开始是否是ab     是返回true
15         System.out.println(str.endsWith("ef"));//判断字符串的结束是否是ef       不是返回false
16     }
17 }

 

posted @ 2016-05-12 15:50  明天会更好!!!!  阅读(218)  评论(0)    收藏  举报