String类的常用方法及小练习

//String常用方法
public class Demo1 {
      public static void main(String[] args) {
//1.测试此字符串是否以指定的前缀开始
//          public boolean startWith(String s)
          String s1 = "hello";
//          判断s1是不是以h开头
          boolean b1 = s1.startsWith("h");
          System.out.println("1:"+b1);
          
          
//2.测试此字符串是否以指定的后缀结束
//          public boolean endWith(String s)
//          判断s1是不是以o结束
          boolean b2 = s1.endsWith("lo");
          System.out.println("2:"+b2);
          
          
//3.按字典顺序比较两个字符串
//          返回值:如果此字符串按字典顺序小于字符串参数,则返回一个小于0的值
//          返回值:如果此字符串按字典顺序大于字符串参数,则返回一个大于0的值
//  public int compareTo(String anotherString)
          String s2 = "ada";
          String s3 = "aab";
//          从第一个不相等的字符进行比较
          System.out.println("3:"+s2.compareTo(s3));
        
          
//4.按字典顺序比较两个字符串,不考虑大小写
//          public int compareToIgnoreCase(Stirng anotherStirng)
          String s4 = "A";
          String s5 = "a";
          System.out.println("4:"+s4.compareToIgnoreCase(s5));
          
          
//5.判断当前对象是否含有参数指定的字符串s
//          public boolean contains(String s)
          String s6 = "abcd";
          System.out.println("5:"+s6.contains("bc"));
          
//6.返回一个字符串,它是通过newChar 替换此字符串中出现的所有oldChar
//          public String replace(char oldChar,char newChar)
          String s7 = "abcaaa";
//          把s7中的a换成d
          System.out.println("6:"+s7.replace("a", "d"));
          
//7.用指定的新字符串,替代当前对象中指定的旧字符串
//          public String replaceAll(String old,String new)
          String s8 = "helloWord";
          System.out.println("7:"+s8.replaceAll("hello", "aaa"));
          
          
//8.将指定字符串拼接到此字符串的结尾
//          public String concat(String str)
          String s9 = "hello";
          System.out.println("8:"+s9.concat("word"));
          
          
//9.返回指定字符串在此字符串中第一次出现处的索引
//          返回值:返回的是索引,找不到返回-1
//          public int indexOf(String s)
          String s10 = "hello";
         System.out.println("9:"+s10.indexOf("h"));
         
//10.截取子字符串,从指定下标开始一直截取到最后
//        public String substring(int statpoint)
         String s11 = "helloword";
         System.out.println("10:"+s11.substring(5));
//11.   截取部分字符串,截取返回区间为[ ),左闭区间右开区间,取不到end
//         public String substring(int starpoint,int endpoint)
         System.out.println("11:"+s11.substring(3, 5));
         
         
//12.返回字符串的副本,忽略前导空白和尾部空白,中间的空白仍保留
//         public String trim()
         String s12 = "   he  llo       ";
         System.out.println("12:"+s12.trim());
         
//13.字符串的长度length()
         String s13 = "he";
         System.out.println("13:"+s13.length());
         
//14.按照给定的规则,将当前字符串拆分成多个字符串
//         public String[] split(String regex)
         String s14 = "today is a goodday";
         String[] str = s14.split(" ");
         for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
        }
    }
}

练习:查找某字符在某个字符串中出现的个数

public class Test2 {
      public static void main(String[] args) {
        int k =0;
        String s2 = "nbaernbatynbauinbaopnba";
        while (true) {
            if (s2.indexOf("nba")<0) {
                break;
            }else {
                s2 = s2.substring(s2.indexOf("nba")+3);
                k++;
            }
        }
        System.out.println(k);
    }
}

猜你喜欢

转载自www.cnblogs.com/lkkkk/p/12186590.html