java:第十五章

第十五章 字符串
1.字符串(String)的定义
        String str="内容";
        String str=new String(); //内容为null
        String str=new String("内容");
     2.java.lang.*
         字符串所在的包是java.lang.
         注意:所有在java.lang包下的类,我们使用时,不需要import。
     3.常用方法
        str.length();//字符串的长度
        str.indexOf(str1);//子串str1出现的下标。(从前往后)
        str.lastIndexOf(str1);//子串str1出现的下标。(从后往前)
     str.substring(begin);//截取从下表begin开始到末尾的子串。
     str.substring(begin,end);//截取下标begin开始到下标end的子串。
      str.trim();//去掉首尾空白
    str.toLowerCase();//返回字符串的小写
      str.toUpperCase();//返回字符串大写

public class *** {
    public static void main(String[] agrs){
        Scanner console = new Scanner(System.in);
        String pwd = console.next();
        //str.lenght();//计算字符串长度
        int length = pwd.length();
        
        System.out.println(length);
    }
}

public class *** {
    public static void main(String[] agrs){
        String str = "adsadsdasdas";
        //indexOf (字符a):返回第一次出现字符a的下标
        //int index = str.indexOf('a');
        //indexOf (字符串):返回第一次出现字符串的下标
        //int index1 = str.indexOf("sa");
        
        //System.out.println(index);
        //System.out.println(index1);
        
        //从后往前找,返回第一次出现的下标
        int li = str.lastIndexOf('a');
        
        int li1 = str.lastIndexOf("sa");
        System.out.println(li);
        System.out.println(li1);
        
    }
}

public class *** {
    public static void main(String[] agrs){
        String str = "我是";
        String str1 = "好人";
        //"+"表示拼接
        String stra = str+str1;
        //字符串拼接
        String strc = str.concat(str1);
        
        System.out.println(stra);
        System.out.println(strc);
    }
}

public class ** {
    public static void main(String[] agrs){
        String str = "abcaaa";
        
        //截取从下标2开始到结尾的字符子串.
        String substr = str.substring(0);
        //截取从下标2开始到下标4的字符子串.包含前面不包含后面
        String substr1 = str.substring(0,1);
        System.out.println(substr);
        System.out.println(substr1);
        
        
        //str = "  abc  ";
        
        //System.out.println(str.length());
        //trim()去首尾空格
        //System.out.println(str.trim().length());
    }
}

public class ***{
    public static void main(String[] agrs){
//        //==:判断两个对象是否都是同一个对象
//        //equals比较内容是否相同
//        
//        String str = "abc";
//        String str1 = str;
//        String str2= new String("abc");
//        System.out.println(str2.equals(str1));
//        System.out.println(str==str2);
        
        String s1 = "abc";
        String s2 = "ABC";
        System.out.println(s1.equals(s2));
        //equalsIgnoreCase忽略大小写的比较
        System.out.println(s2.equalsIgnoreCase(s2));
        //toLowerCase转换成小写
        String s11 = s1.toLowerCase();
        System.out.println(s11);//abc
        //toUpperCase转换成大写
        String s12 = s1.toUpperCase();
        System.out.println(s12);//ABC
    }
}
原文地址:https://www.cnblogs.com/yangchan250/p/6961039.html