包装常用方法,Random类,String类,StringBuffer类,Date类,Calendar类

包装类的常用方法:

XXXValue():包装类转换成基本类型

Byte byte1 = new Byte("12"); byte num1 = byte1.byteValue(); System.out.println(num1);

toString():以字符串形式返回包装对象表示的基本类型数据(基本类型—>字符串)

            `   byte num4 = 10;
        String str1 = Byte.toString(num4);
        System.out.println(str1);
    
        String str2= Boolean.toString(false);
        System.out.println(str2);
    `

parseXXX():把字符串转换为相应的基本数据类型(Character除外)(字符串-->基本数据)

`int num5 = Integer.parseInt("123456");
    System.out.println(num5);
    boolean num6= Boolean.parseBoolean("true");
    System.out.println(num6);
    
    `

valueOf():

所有包装类都有如下方法(基本类型->包装类): public static Type valueOf(type value)如:

                `Integer int1= Integer.valueOf(1520);
                    System.out.println(int1);`

除Character类外,其他包装类都有如下方法(字符串->包装类):public static Type valueOf(String s )

Long lo1= Long.valueOf("200"); System.out.println(lo1);

装箱和拆箱:基本类型和包装类的自动类型转换

        装箱:基本数据类型转换为包装类的对象

byte num1 = 10;

Byte byte1 = num1;

        拆箱:包装类对象转换为基本类型的值

Integer int2= new Integer("255");
	int num3 = 300;
	int result = int2+num3;
	Integer int3=int2+num3;
	System.out.println(result);
	System.out.println(int3);
	`

Math类

提供了常用的数学运算方法和两个静态常量E(自然对数的底数)和PI(圆周率)

    //随机数的方法:Math.random():随机返回一个[0,1)之间的double数
    double num1 =Math.random();
    System.out.println(num1);
    
    //返回一个[num1,num2)的随机数
    //int nums = (int)(Math.random()*(num2-num1)+num1);`

Random类:

生成随机数的其他方法:

// 创建Random对象 Random random = new Random(); for (int i = 0; i < 20; i++) { int num = random.nextInt(10); System.out.println("第"+(i+1)+"个随机数是:"+num); }

String类:

String类位于java.lang包中,具有丰富的方法:计算字符串的长度,比较字符串,链接字符串,提取字符串

获取字符串的长度

String string1 = "qwertyuiop"; //字符串方法 //获取字符串的长度 int num = string1.length(); System.out.println("字符串的长度为:"+num);

/*

  • 获取数组的长度:数组名.length //这是属性

    • 获取集合的长度:集合名.size()//调用方法

      • 获取字符串的长度:字符串对象名.length() */

比较两个字符串内容相同

String str1 = "qwe"; String str2 = "qwe"; System.out.println(str1.equals(str2)); System.out.println(str1==str2);//==比较的是字符串地址值,equals比较的是字符串内容

字符串其他比较方法:

使用equalsIgnoreCase()方法 :不区分大小写

使用toLowerCase()方法:大写转小写

使用toUpperCase()方法:小写转大写

String str5 = "qwert";
        String str6 = "QWERT";
        System.out.println(str5.equals(str6));//区分大小写
        //equalslgnoreCase()不区分大小写比较
        System.out.println(str5.equalsIgnoreCase(str6));//true
        
        String result = str6.toLowerCase();
        System.out.println(str6);//QWERT//字符串对象调用方法后,不会改变自身的内容,相当于是对它的复制品进行了修改
        System.out.println(result);//大写转换小写qwert
        
        System.out.println(str5.toUpperCase());//小写转换大写

字符串连接

1.使用“+”

2.使用String类的concat()方法

public int indexOf(int ch) 、public int indexOf(String value):: 搜索第一个出现的字符串ch(或字符串value)如果没有找到,返回-1;

        String str1 = "abcdefghijklmnabcd";
        System.out.println(str1.length());//18
        int index= str1.indexOf(97);
        System.out.println(index);
        System.out.println(str1.indexOf("cd")); 

/public int lastIndexOf(int ch),public int lastIndexOf(String value):搜索最后一个出现的字符ch(或者字符串value),如果没有找到,返回-1;

        int index1 =str1.lastIndexOf(100);
        System.out.println(index1);
        System.out.println(str1.lastIndexOf("mn"));
        

public String substring(int index):提取从位置索引开始的字符串部分

        String str2 = str1.substring(5);
        System.out.println(str2);

public String substring(int beginindex,int endindex)提取beginindex(包括)和endindex(不包括),之间的字符串部分

        String str3 = str1.substring(3, 12);
        System.out.println(str3);

public String trim() :返回一个前后不含任何空格的字符串的副本

        String str4 = "    qwert    yuiop    ";
        System.out.println(str4);
        System.out.println(str4.trim());//qwert    yuiop

字符串的拆分

        String str5 = "长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
        String[] strs= str5.split(" ");
        for (String string : strs) {
            System.out.println(string);
        }

判断字符串是否以指定内容结尾

        String str7 = "HelloWorld.java";
        System.out.println(str7.endsWith(".java"));

判断字符串是否以指定的内容开头

        System.out.println(str7.startsWith("he"));

将字符串变成byte类型的数组

    byte[] bytes= str7.getBytes();
        for (byte b : bytes) {
            System.out.print((char)b);
        }
        

StringBuffer类:

           *String 是不可变的对象
     *      经常改变内容的字符串最好不要使用String
     *      StringBuffer是可变的字符串
     *      字符串经常改变的情况可使用StringBuffer,更高效
     *      JDK5.0后提供了StringBuilder,等价StringBuffer

StringBuffer 对字符串频繁修改(如字符串链接)时,使用StringBuffer类可以大大提高程序执行效率

sb.toString(); //转化为String类型

System.out.println(sb1.toString());

sb.append("**"); //追加字符串

StringBuffer sb3 =sb1.append(str1); System.out.println(sb3); System.out.println(sb1);

sb.insert (1, "**"); //插入字符串

System.out.println(sb1.insert(5, str1)); System.out.println(sb1);

操作当前日期

java.util.Date类:表示日期和时间:提供操作日期和时间各组成部分的方法

java.text.SimpleDateFormat类

        Date date = new Date();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E D");
        String str= sdf.format(date);
        System.out.println(str);

Calendar类

抽象类,java.util.Calendar。用于设置和获取日期/时间数据的特定部分

//获取年
        int year= cl.get(Calendar.YEAR);
        System.out.println(year);
        //获取月
        int month= cl.get(Calendar.MONTH);
        System.out.println(month+1);
        //获取日
        int date = cl.get(Calendar.DATE);
        System.out.println(date);
        
        //获取时分秒
        System.out.println(cl.get(Calendar.HOUR)+":"+cl.get(Calendar.MINUTE)+":"+cl.get(Calendar.SECOND));
        
        System.out.println(year+"-"+(month+1)+"-"+date);
        
        
        //获取星期
        int day_of_week= cl.get(Calendar.DAY_OF_WEEK);
        System.out.println(day_of_week-1);
        
        //获取当前日期是这一年中的多少天
        System.out.println(cl.get(Calendar.DAY_OF_YEAR));

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值