23 String类常用方法

package com.bowen.dong;
/**
 * 1  String类的基本操作
 *      获取长度  public int length();
 *      public char charAt(int index);
 *      public int indexOf(char a);
 *      public int lastIndexOf(char a);
 *      public int indexOf(String str);获取指定子串在字符串第一次出现的位置
 * @author bw
 *
 */
public class Example005 {
   public static void main(String[] args) throws Exception {
     
      String str1 = "abccc11ssaa";  
      //字符常量的长度
      System.out.println(str1.length());
      //获取字符串对应位置上的字符
      System.out.println(str1.charAt(0));
      //c第一次出现的位置
      System.out.println(str1.indexOf('c'));
      //获取字符最后出现的位置
      System.out.println(str1.lastIndexOf('c'));
      //
      System.out.println(str1.indexOf("cc"));
      //
      System.out.println(str1.lastIndexOf("abc"));
   }
}

猜你喜欢

转载自www.cnblogs.com/sunnybowen/p/9860187.html
23