Ⅴ.字符串

1. 基本类的封装

byte Byte
short Short
int Integer
long Long
float Float
double Double

 int i = 5;
        Integer it = new Integer(i);
        //Integer是Number的子类,所以打印true
        System.out.println(it instanceof Number);

        //基本类型转换成封装类型
        Integer it = new Integer(i);
        //封装类型转换成基本类型
        int i2 = it.intValue();
         
        //自动转换就叫装箱
        int i = 5;
        Integer it2 = i;

        //封装类型转换成基本类型
        int i2 = it.intValue();
         
        //自动转换就叫拆箱
        int i3 = it;

        //int的最大值
        System.out.println(Integer.MAX_VALUE);
        //int的最小值      
        System.out.println(Integer.MIN_VALUE);

2. 字符串转换

#数字转字符串
int i = 5;
        //方法1
        String str = String.valueOf(i);
        //方法2
        Integer it = i;
        String str2 = it.toString();


#字符串转数字
        String str = "999";
        int i= Integer.parseInt(str);
        System.out.println(i);

3. 常用Math方法

        //5.4四舍五入即5
        System.out.println(Math.round(f1));
        //5.5四舍五入即6
        System.out.println(Math.round(f2));
         
        //得到一个0-1之间的随机浮点数(取不到1)
        System.out.println(Math.random());
         
        //得到一个0-10之间的随机整数 (取不到10)
        System.out.println((int)( Math.random()*10));
        //开方
        System.out.println(Math.sqrt(9));
        //次方(2的4次方)
        System.out.println(Math.pow(2,4));
         
        //π
        System.out.println(Math.PI);
         
        //自然常数
        System.out.println(Math.E);

4. 格式化输出

%s 表示字符串
%d 表示数字
%n 表示换行

printf=format
        int year = 2020;
        //总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
          
        //直接打印数字
        System.out.format("%d%n",year);
        //总长度是8,默认右对齐
        System.out.format("%8d%n",year);
        //总长度是8,左对齐
        System.out.format("%-8d%n",year);
        //总长度是8,不够补0
        System.out.format("%08d%n",year);
        //千位分隔符
        System.out.format("%,8d%n",year*10000);
  
        //小数点位数
        System.out.format("%.2f%n",Math.PI);
          
        //不同国家的千位分隔符
        System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);

5. 字符

字符类
Character

        char c1 = 'a';
        Character c = c1; //自动装箱
        c1 = c;//自动拆箱

        System.out.println(Character.isLetter('a'));//判断是否为字母
        System.out.println(Character.isDigit('a')); //判断是否为数字
        System.out.println(Character.isWhitespace(' ')); //是否是空白
        System.out.println(Character.isUpperCase('a')); //是否是大写
        System.out.println(Character.isLowerCase('a')); //是否是小写
         
        System.out.println(Character.toUpperCase('a')); //转换为大写
        System.out.println(Character.toLowerCase('A')); //转换为小写
 
        String a = 'a'; //不能够直接把一个字符转换成字符串
        String a2 = Character.toString('a'); //转换为字符串

        System.out.println("使用空格无法达到对齐的效果");
        System.out.println("abc def");
        System.out.println("ab def");
        System.out.println("a def");
          
        System.out.println("使用\\t制表符可以达到对齐的效果");
        System.out.println("abc\tdef");
        System.out.println("ab\tdef");
        System.out.println("a\tdef");
         
        System.out.println("一个\t制表符长度是8");
        System.out.println("12345678def");
          
        System.out.println("换行符 \n");
        System.out.println("abc\ndef");
 
        System.out.println("单引号 \'");
        System.out.println("abc\'def");
        System.out.println("双引号 \\"");
        System.out.println("abc\"def");
        System.out.println("反斜杠本身 \\");
        System.out.println("abc\\def");

6. 字符串

字符串即字符的组合,在Java中,字符串是一个类,所以我们见到的字符串都是对象 。
常见创建字符串手段:

  1. 每当有一个字面值出现的时候,虚拟机就会创建一个字符串
  2. 调用String的构造方法创建一个字符串对象
  3. 通过+加号进行字符串拼接也会创建新的字符串对象

String 被修饰为final,所以是不能被继承的
String 的表现就像是一个常量,创建出来的字符串不能更改

length()方法返回字符串长度

charAt()  获取字符
        String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
        char c = sentence.charAt(0);
        System.out.println(c);

toCharArray()  获取对应字符数组
        String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
        char[] cs = sentence.toCharArray(); //获取对应的字符数组
        System.out.println(sentence.length() == cs.length);

subSrting()  截取字符串
        String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
        //截取从第3个开始的字符串 (基0)
        String subString1 = sentence.substring(3);
        System.out.println(subString1);
        //截取从第3个开始的字符串 (基0)
        //到5-1的位置的字符串
        //左闭右开
        String subString2 = sentence.substring(3,5);

split()  分割字符串
        String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
        //根据,进行分割,得到3个子字符串
        String subSentences[] = sentence.split(",");
        for (String sub : subSentences) {
            System.out.println(sub);

trim() 去掉首位空格
        String sentence = "        盖伦,在进行了连续8次击杀后,获得了 超神 的称号      ";
        System.out.println(sentence);
        //去掉首尾空格
        System.out.println(sentence.trim());

toLowcase() 转换为小写
toUpperCase() 转换为大写

indexOf()
lastIndexOf()  
contains()  定位
        String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
        System.out.println(sentence.indexOf('8')); //字符第一次出现的位置
        System.out.println(sentence.indexOf("超神")); //字符串第一次出现的位置
        System.out.println(sentence.lastIndexOf("了")); //字符串最后出现的位置
        System.out.println(sentence.indexOf(',',5)); //从位置5开始,出现的第一次,的位置
        System.out.println(sentence.contains("击杀")); //是否包含字符串"击杀"

replaceAll()  
replaceFirst()  替换
        String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
        String temp = sentence.replaceAll("击杀", "被击杀"); //替换所有的
        temp = temp.replaceAll("超神", "超鬼");
        System.out.println(temp);
        temp = sentence.replaceFirst(",","");//只替换第一个
        String str1 = "the light";
        String str2 = new String(str1);
        //==用于判断是否是同一个字符串对象:false
        System.out.println( str1  ==  str2);

        #String如果发现已经创建了字符串会直接拿来用,而不会创建新的。
        String str1 = "the light";
        String str3 = "the light";
        //==用于判断是否是同一个字符串对象:true
        System.out.println( str1  ==  str3);

        String str1 = "the light";
        String str2 = new String(str1);
        String str3 = str1.toUpperCase();
        //==用于判断是否是同一个字符串对象:false
        System.out.println( str1  ==  str2);
        System.out.println(str1.equals(str2));//完全一样返回true
        System.out.println(str1.equals(str3));//大小写不一样,返回false
        System.out.println(str1.equalsIgnoreCase(str3));//忽略大小写的比较,返回true

        String str1 = "the light";
        String start = "the";
        String end = "Ight";
        System.out.println(str1.startsWith(start));//以...开始
        System.out.println(str1.endsWith(end));//以...结束

7. StringBuffer

append追加 
delete 删除 
insert 插入 
reverse 反转
        String str1 = "let there ";
        StringBuffer sb = new StringBuffer(str1); //根据str1创建一个StringBuffer对象
        sb.append("be light"); //在最后追加
        System.out.println(sb);
        sb.delete(4, 10);//删除4-10之间的字符
        System.out.println(sb);
        sb.insert(4, "there ");//在4这个位置插入 there
        System.out.println(sb);
        sb.reverse(); //反转

length 长度
capacity 空间
        String str1 = "the";
        StringBuffer sb = new StringBuffer(str1);
        System.out.println(sb.length()); //内容长度
        System.out.println(sb.capacity());//总空间:比长度多16

8. 日期

类名
java.util.Date;

        // 当前时间
        Date d1 = new Date();
        System.out.println("当前时间:");
        System.out.println(d1);
        System.out.println();
        // 从1970年1月1日 早上8点0分0秒 开始经历的毫秒数
        Date d2 = new Date(5000);
        System.out.println("从1970年1月1日 早上8点0分0秒 开始经历了5秒的时间");
        System.out.println(d2);

        Date now= new Date();
        //打印当前时间
        System.out.println("当前时间:"+now.toString());
        //getTime() 得到一个long型的整数
        //这个整数代表 1970.1.1 08:00:00:000,每经历一毫秒,增加1
        System.out.println("当前时间getTime()返回的值是:"+now.getTime());
        //通过System.currentTimeMillis()获取当前日期的毫秒数
        System.out.println("System.currentTimeMillis() \t返回值:"+System.currentTimeMillis());
日期转字符串
        //y 代表年
        //M 代表月
        //d 代表日
        //H 代表24进制的小时
        //h 代表12进制的小时
        //m 代表分钟
        //s 代表秒
        //S 代表毫秒
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" );
        Date d= new Date();
        String str = sdf.format(d);
        System.out.println("当前时间通过 yyyy-MM-dd HH:mm:ss SSS 格式化后的输出: "+str);
         
        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
        Date d1= new Date();
        String str1 = sdf1.format(d1);
        System.out.println("当前时间通过 yyyy-MM-dd 格式化后的输出: "+str1);

字符串转日期
    public static void main(String[] args) {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss" );
  
        String str = "2016/1/5 12:12:12";
          
        try {
            Date d = sdf.parse(str);
            System.out.printf("字符串 %s 通过格式  yyyy/MM/dd HH:mm:ss %n转换为日期对象: %s",str,d.toString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
Calendar与Date进行转换
    public static void main(String[] args) {
        //采用单例模式获取日历对象Calendar.getInstance();
        Calendar c = Calendar.getInstance();
          
        //通过日历对象得到日期对象
        Date d = c.getTime();
  
        Date d2 = new Date(0);
        c.setTime(d2); //把这个日历,调成日期 : 1970.1.1 08:00:00

翻日历
        Calendar c = Calendar.getInstance();
        Date now = c.getTime();
        // 当前日期
        System.out.println("当前日期:\t" + format(c.getTime()));
 
        // 下个月的今天
        c.setTime(now);
        c.add(Calendar.MONTH, 1);
        System.out.println("下个月的今天:\t" +format(c.getTime()));
 
        // 去年的今天
        c.setTime(now);
        c.add(Calendar.YEAR, -1);
        System.out.println("去年的今天:\t" +format(c.getTime()));
 
        // 上个月的第三天
        c.setTime(now);
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DATE, 3);
        System.out.println("上个月的第三天:\t" +format(c.getTime()));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值