字符串的一些方法

创建一个String类的对象str1,调用一些方法,看看效果:

 1 public class String01 {
 2     public static void main(String[] args)
 3     {
 4         String str1 = new String("ZJU");
 5         System.out.println(str1.indexOf("JU"));  //这个方法返回的是子字符串首字母的索引,从前向后读
 6         System.out.println(str1.lastIndexOf("JU"));  //从后向前读,返回读到的第一个这样的子串的首字母索引
 7         System.out.println(str1.indexOf("U"));   //一个字母也可看成字符串
 8         System.out.println(str1.length());  //返回字符串长度
 9     }
10 }

运行效果如下:

 

posted @ 2021-01-25 11:37  EvanTheBoy  阅读(40)  评论(0)    收藏  举报