zoukankan      html  css  js  c++  java
  • Java学习笔记(4)字符串常用方法、包装类型、枚举

    一、字符串常用方法

    和python字符串的那些方法差不多,有的名字不一样而已

    package Java基础.字符串常用方法;
    
    import java.util.Arrays;
    public class StringMethod {
        //字符串常用方法
        public static void main(String[] args) {
            String name = "xiaohei";
            System.out.println("equals:" + name.equals("xiaohei")); // 判断两个字符串是否一样
            System.out.println("equalsIgnoreCase:"+ name.equalsIgnoreCase("xiaoHei"));// 忽略大小写,判断两个字符串是否一样
            System.out.println("contains:"+ name.contains("x"));// 判断是否含有某个字符串,和python的 in一样
            System.out.println("startWith"+name.startsWith("x"));// 以 xx 开头
            System.out.println("toUpperCase:"+name.toUpperCase());// 大写
            System.out.println("toLowerCase:"+name.toLowerCase());// 小写
            System.out.println("endsWith:"+name.endsWith("i"));// 以xxx字符串结尾
            System.out.println("indexOf:"+name.indexOf("i"));// 返回字符串第一次出现的下标,如果没有就返回-1
            System.out.println("lastIndexOf:"+name.lastIndexOf("i"));// 返回字符串最后一次出现的下标,如果没有就返回-1
            System.out.println("substring:"+name.substring(2));// 类是python的切片操作,传一个参数时,如传2,返回切掉0 1下标的字符后剩余的字符串
            System.out.println("substring:"+name.substring(2,5));// 当传两个参数时,第一个是从哪里开始取,第二个是取到哪里结束,也是顾头不顾尾,没有第三个步长参数
    
            String str = " hello,kugou ";
            System.out.println("trim:" + str.trim()); // 去掉前后空格和字符串,和python 的strip一样,不像pyton的lstrip和rstrip
            System.out.println("isEmpty:" + str.isEmpty());// 判断是否为空字符串
            System.out.println("replace:" + str.trim().replace('h', 'H'));// 替换字符replace
            System.out.println("replaceAll:" + str.trim().replaceAll("h", "H"));// 支持正则替换
            System.out.println("split:" + str.trim().split(","));// 先去空格再分割
            // 分割字符串,和python的split一样,返回的是一个数组,java里的数组不能直接print,要用Array.toString
            String sp_list[] = str.trim().split(",");
            System.out.println(Arrays.toString(sp_list));
    
            String names[] = {"xiaohei","xiaobai","xiaohang","xiaolan"};// 字符串列表
            System.out.println("join = "+ String.join(",", names));// 拼接字符串,用的String.  不是字符串自带的方法
    
            // 类型转换
            // 要把其他类型转换为字符串,可以使用静态方法valueOf().这是一个重载方法,会根据参数自动选择合适的方法;
            System.out.println("valueOf int:"+String.valueOf(123));
            System.out.println("valueOf float:"+String.valueOf(45.6));
            System.out.println("valueOf boolean:"+String.valueOf(true));
            System.out.println("valueOf 数组:"+String.valueOf(names));
    
            // 字符串转其他类型
            int n1 = Integer.parseInt("123"); // ”123“转为int
            System.out.println("n1:"+n1);
            int n2 = Integer.parseInt("ff",16); // 按十六进制转换,255
            System.out.println("n2:"+n2);
    
            boolean b1 = Boolean.parseBoolean("true");// true
            System.out.println("b1:"+b1);
            boolean b2 = Boolean.parseBoolean("False");// false
            System.out.println("b2:"+b2);
    
    
        }
    }

    二、包装类型

    包装说白了就是自己对基本类型或者java现有类型封装的一个类,自己可以多给他加一些功能。比如说下面我自己写的一个包装类,MyString,就加了isdigits的函数,和python字符串的方法一样,判断是否为纯整数。

    package day4;
     
    public class MyString {
        private String str;
        public MyString(String str){
            this.str = str;
        }
        public String valueString(){
            return this.str;
        }
        public boolean isDigits(){
            //判断是否为整数
            try {
                Integer.parseInt(this.str);
            } catch (Exception e){
                return false;
            }
            return true;
        }
    }

    使用的时候如下代码,包装类型就是对现有类型做的一个封装,增加一些更好用的功能,java中默认对所有的基本类型都有包装类型,如下图:

    package day4;
     
    public class Main {
        public static void main(String[] args) {
     
            //自己包装的MyString类
            MyString choice = new MyString("3");
            System.out.println(choice.isDigits());
            MyString choice2 = new MyString("2.5");
            System.out.println(choice2.isDigits());
            String choice3 = choice.valueString(); //转成String
            
            Integer age = new Integer(78);//
            float f = age.floatValue();   //Integer包装类自带的转float类型方法
            Integer money = 54; //下面这种和上面效果是一样的,java在编译的时候自动装箱,把54变成Inter类型
            //但是自己写的类,比如说我上面的MyString就不能用这种方法了,java编译器是识别不了的
            float f2 = money.floatValue();
     
    //        int m  = 54;
    //        Integer money = Integer.valueOf(m); 上面的 Integer money = 54;就和这2行代码一样
     
     
        }
    }

     // 包装类
            // 在实际工作中,8种基本数据类型已经不满足要求了,经常方法需要传一个对象,所以需要对8种数据进行封装成一个类
    //        Integer i = new Integer(123);// 装箱,int 变成Integer类的对象
            Integer i = new Integer("123");// 装箱,int 变成Integer类的对象
            int i0 = i.intValue(); // 拆箱,将Integer类的对象还原为基础数据类型 int型
            System.out.println("Integer i:"+i);
            System.out.println("int i0:"+i0);
    
            //jdk 1.5后之后支持自动装箱,拆箱
            Integer i1 = 123; //自动装箱
            int i2 = i1; // 自动拆箱
            System.out.println("i1:"+i1);
            System.out.println("i2:"+i2);
    
            // 将string 类型转换为基本数据类型,使用parseXxx方法
            int x = Integer.parseInt("123");
            float f = Float.parseFloat("0.123");
            boolean b = Boolean.parseBoolean("TRUE");
            // 将基本数据类型转换为string类型,使用valueOf方法
            String istr = String.valueOf(x);
            String fstr = String.valueOf(f);
            String bstr = String.valueOf(b);

    三、枚举类型

    枚举类型就是只有那几个值,值必须再这几个之间,否则就会报错,说白了就是几个常量。

    package day4;
     
    public class EnumClass {
        public static void main(String[] args) {
            Sex sex = Sex.NV;
            System.out.println(sex.ordinal());//编号,按照顺序来的
            System.out.println(sex.name());//String类型名字
     
     
            Sex2 xb = Sex2.NAN;
            System.out.println(xb.name()); //返回的是字符串NAN
            System.out.println(xb.id); //编号,这样的话,就指定了编号,就不用非得按照顺序了
     
            Sex3 xingbie = Sex3.NAN;
            System.out.println(xingbie.name()); //返回的是字符串NAN
            System.out.println(xingbie.id); //编号,这样的话,就指定了编号,就不用非得按照顺序了
            System.out.println(xingbie.chinese_name); //这样就自己增加了一个属性
     
        }
     
    }
     
    //枚举类型,就是限定死了几个值,用的时候只能在这几个值里面
    enum Sex { NAN,NV}
     
    enum Sex2 {
        //因为enum本身也是一个类,加上构造方法,NAN(0),实例化的时候就传入了一个数字,
        // 这样的话编号就是自己指定的
        NAN(0),NV(1);
        public final int id;
     
        private Sex2(int sexValue){
            this.id = sexValue;
        }
    }
     
    //这样可以给他加其他的属性
    enum  Sex3 {
        NAN(0,"男"),NV(1,"女");
        public final int id;
        public final String chinese_name;
        private Sex3(int id,String chinese_name){
            this.id = id;
            this.chinese_name = chinese_name;
        }
    }

    转载自:http://www.nnzhp.cn/archives/870

  • 相关阅读:
    stm32f103和stm32f407的GPIO口模式设置以及相互对应的关系
    基于STM32单片机实现屏幕休眠后OLED屏幕滚动效果
    基于51单片机的超声波模块HC-SR04的使用
    用51单片机控制L298N电机驱动模块
    学习笔记——51单片机 单片机与单片机之间的通讯
    基于51单片机的电子密码锁—1
    LCD1602学习调试
    基于51单片机,通过定时器实现的时钟程序
    有进度条圆周率计算
    python turtle 学习笔记
  • 原文地址:https://www.cnblogs.com/bugoobird/p/13694622.html
Copyright © 2011-2022 走看看