zoukankan      html  css  js  c++  java
  • Java1:Chapter15

    第十五章 字符串
    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
        }
    }
  • 相关阅读:
    java类继承总结一 父类类型与子类类型之间的转化问题(转)
    将子类对象引用赋值给超类对象 JAVA 编译时多态性
    JAVA访问控制变量、类变量、类方法
    java传递是引用的拷贝,既不是引用本身,更不是对象
    JAVA的StringBuffer类
    (文件名.JAVA)的文件名只能与该文件中的public类的名称一致
    类变量(静态变量)的值不能被构造函数改写
    程序启动的顺序以及实例变量相互赋值、传递拷贝的理解
    MySQL选择合适的字符集
    MySQL日期类型选择
  • 原文地址:https://www.cnblogs.com/wangjinshabi250/p/6964665.html
Copyright © 2011-2022 走看看