十二章常用类

来源:互联网 发布:怎么设置淘宝会员名 编辑:程序博客网 时间:2024/05/18 00:07


1、String
     字符串常量例如 "hello"是一个对象,都会预先加载在字符串常量池中

     String对象 的内容不可改变,对String做改变都是产生一个新对象给你
     String str0 = "hello"; 
    ①、与字符数组相关
           char[] charArray = str0.toCharArray();
           char c = str0.charAt(1);
           int index = str0.indexOf('l');//返回一个字符在String中首次出现的位置
           index = str0.lastIndexOf('l');//返回一个字符在String中最后出现的位置
           int length = str0.length();
           boolean flag = str0.contains("ell");//返回一个字符串是否包含另一个字符串
           String subString = str0.substring(1, 4);//根据下标截取字符串(前闭后开)
          subString = str0.substring(2);
          String newStr = str0.replace('l', 'L');//替换字符
          newStr = str0.replace("ll", "TT");替换字符串
          flag = str0.startsWith("he");//判断一个字符串是否以某个字符串开头
          flag = str0.endsWith("lo");//判断一个字符串是否以某个字符串结束
          String str4 = str0.concat("world");//连接


   ②、与英文相关的
          String upCase = str0.toUpperCase();//转全大写
          String lowCase = upCase.toLowerCase();//转全大写
          boolean flag1 = "hello".equalsIgnoreCase("Hello");//忽略大小写比较相等
          System.out.println("hello".compareTo("Hello"));//使用字典顺序进行判断
          System.out.println("hello".compareToIgnoreCase("Hello"));
  
    ③、重点方法
          对任意一个字符串做非空判断,都要做两个判断(空与空串),而且顺序不能变
          str0 != null && !str0.equals("")
  
          trim()方法--去掉一个字符串前后空格
                  str0 = "    he  llo    ";
                  System.out.println(str0.trim() + "!");
  
         matches()--正则表达式匹配
                  任意一个字符串都是正则表达式

                  [ ] :代表一个字符,内部书写有效的选择

                  { } :代表正则表达式反复出现的次数

                          {m,n} :最少出现m次,最多出现n次

                          {m,} :最少出现m次,最多不限

                          {m} : 只能出现m次

                          *,+,?也是用来表示前面表达式出现的次数

                          *:任意次            +:至少一次,至多不限           ?:0---1次

                  (|):代表可选择的表达式
  
         split()--对字符串进行拆分
                 String message = "J122?123456?10000.0";
                 String[] results = message.split("\\?");
  
  
       StringBuffer--内容可变的字符串类型,主要的用途就是用在改变StringBuffer对象的内容
                         ---适用于多线程,设计为线程安全,但执行效率低
       StringBuilder--内容可变的字符串类型,主要的用途就是用在改变StringBuilder对象的内容
                           ---适用于单线程,不考虑线程安全,执行效率高
        在大量需要做字符串拼接的时候,我们选用这两个类
               StringBuffer sb = new StringBuffer("hello");
               sb.append("world");//在sb对象的末尾添加world
               sb.insert(5, "你个");
               sb.reverse(); //反转

        性能:StringBuilder > StringBuffer > String(丰富度高)


2、包装类(8个,在基本数据类型与引用数据类型之间充当桥梁)

     ①、基本转包装类对象
             int a = 10;
             Integer i = new Integer(a);
             Integer i = a;//自动封箱--JDK1.5以后
  
    ②、包装类对象转基本
            Integer i = 100;
            int a = i;//自动拆箱---JDK1.5以后
           int a = i.intValue();
  
    ③、包装类对象转String
           Integer i = 200;
           String str = i.toString();
           str = i + "";//其实本质还是调用了Integer对象的toString()
  
    ④、String转包装类对象
           String str = "300";
           Integer i = new Integer(str);

    ⑤、基本转String
           int i = 400;
          String str = Integer.toString(i);
          str = i + "";
  
    ⑥、String转基本--用得最多!
          String str = "250";
          int i = Integer.parseInt(str);
 

3、日期类

     取当前日期,通常使用Date
            Date date = new Date();
     使用SimpleDateFormat类进行格式化输出
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a hh:mm:ss E");
            String dateMsg = sdf.format(date);
  
      当涉及到具体某个日期的设置或获取的时候,通常使用Calender

  

     properties   特点:①集合类  ②可以操作文件

     作为集合类是特点:①可变大变小    ②存放不占用连续空间       ③可存放object类型,但是操作文件时数据都被作为String类型

                                     ④数据在内部按键值对的方式存放

      产生容器对象
      Properties props = new Properties();
     放入数据--键值对
     props.setProperty("username", "zhang3");
     props.setProperty("password", "123453");
     修改数据--给已有键赋新值
     props.setProperty("password", "6543213");
     根据键取出值
     String value = props.getProperty("password");
     根据键移除数据
     props.remove("password");
     得到容器中数据的数量
     int size = props.size();

    把数据写入文件--如果文件不存在,会自动新建一篇文件放入数据
     try {
         props.store(new FileOutputStream("data.properties"), "这是一个用户的信息");
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }

     把数据读入Properties对象--如果文件不存在,会报FileNotFoundException异常
     Properties newProps = new Properties();
     try {
          newProps.load(new FileInputStream("data.properties"));
     } catch (FileNotFoundException e) {
           e.printStackTrace();
     } catch (IOException e) {
          e.printStackTrace();
     }




0 0