数字与字符串

7.1 封装类(装箱与拆箱)

所有的基本类型都有对应的封装类

数字封装类有 Byte,Short,Integer,Long,Float,Double 
这些类都是抽象类Number的子类

基本类型与封装类的转换

public class Test {

    public static void main(String[] args) {

       int i=5;

       //基本数据类型转换成封装类

       Integer in=new Integer(i);

       //封装类型转换成基本类型

       int a=in.intValue();

    }

}

自动装箱与拆箱

不需要调用构造方法,通过=符号自动把 基本类型 转换为 类类型 就叫装箱

不需要调用Integer的intValue方法,通过=就自动转换成int类型,就叫拆箱

public class Test {

    public static void main(String[] args) {

       int i=5;

       //基本数据类型转换成封装类

       Integer in=new Integer(i);

       // 自动转换 装箱

       Integer a=i;

       // 自动转换 拆箱

       int b=in;

    }

}

int的最大值,最小值

Integer.MAX_VALUE

Integer.MIN_VALUE

7.2 字符串与数字的转化

数字转字符串

方法1: 使用String类的静态方法valueOf 
方法2: 先把基本类型装箱为对象,然后调用对象的toString

字符串转数字

调用Integer的静态方法parseInt

public class Test {

    public static void main(String[] args) {

       int i=5;

       // 数字转换成字符串类型 方法1

       String str1 = String.valueOf(i);

       // 数字转换成字符串类型 方法2

       Integer in =i;

       String str2 = in.toString();

       // 字符串转换为数字类型

       String str="55";

       int a=Integer.parseInt(str);

       System.out.println(a);

    }

}

7.3 Math类常用方法

public class Test {

    public static void main(String[] args) {

       //随机数 [0-1)

       System.out.println(Math.random());

       //四舍五入

       System.out.println(Math.round(5.4)); // 5

       //开方

       System.out.println(Math.sqrt(9));  // 3.0

       //次方

       System.out.println(Math.pow(2, 3)); // 8.0

       //常数π

       System.out.println(Math.PI); // 3.141592653589793

       //自然常数

       System.out.println(Math.E);

    }

}

7.4 格式化输出

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

printf和format能够达到一模一样的效果,如何通过eclipse查看java源代码 可以看到,在printf中直接调用了format

public class Test {

    public static void main(String[] args) {

       String name="Tom";

       int i =5;

       String a="%s已经第%d次迟到了%n哈哈";

       //使用printf

       System.out.printf(a,name,i);

       //使用format

       System.out.println();

       System.out.format(a,name,i);

    }

}

换行符

换行符就是另起一行 --- '\n' 换行(newline)
回车符就是回到一行的开头 --- '\r' 回车(return)
在eclipse里敲一个回车,实际上是回车换行符
Java是跨平台的编程语言,同样的代码,可以在不同的平台使用,比如Windows,Linux,Mac
然而在不同的操作系统,换行符是不一样的
(1)在DOS和Windows中,每行结尾是 “\r\n”;
(2)Linux系统里,每行结尾只有 “\n”;
(3)Mac系统里,每行结尾是只有 "\r"。
为了使得同一个java程序的换行符在所有的操作系统中都有一样的表现,使用%n,就可以做到平台无关的换行

7.5 操纵字符串

保存一个字符的时候使用char

把字符,数字转换为字符串先转换为对应的包装类在调用toString方法

    public static void main(String[] args) {

       char a='中';

       char b='1';

       Character c=a; // 自动装箱

       a=c;  //自动拆箱

       //把字符转换为字符串先转换为对应的包装类在调用toString方法

       String str=Character.toString(a);     

    }

}

7.6 操纵字符串

7.6.1 获取字符

charAt(int index)获取指定位置的字符

7.6.2 获取对应的字符数组

toCharArray() 获取对应的字符数组

7.6.3截取子字符串

subString 
截取子字符串

7.6.4 分隔

split 
根据分隔符进行分隔

7.6.5 去掉首尾空格

trim 
去掉首尾空格

7.6.6 大小写

toLowerCase 全部变成小写 
toUpperCase 全部变成大写

7.6.7 定位

indexOf 判断字符或者子字符串出现的位置
contains 是否包含子字符串

7.6.8 替换

replaceAll 替换所有的 
replaceFirst 只替换第一个

public class Test {

    public static void main(String[] args) {

       String a="河南省,郑州市,金水区";

       System.out.println(a.charAt(0)); //

       // subString 截取字符串 左闭右开

       String b=a.substring(0, 2); // 河南

       System.out.println(b);

       // 以 ,分割 得到三个子字符串

       String[] str=a.split(",");

       for (String string : str) {

           System.out.println(string);

       }

    }

}

7.7 比较字符串

使用equals进行字符串内容的比较,必须大小写一致 
equalsIgnoreCase,忽略大小写判断内容是否一致

public class Test {

    public static void main(String[] args) {

       String a="河南省郑州市金水区aa";

       String b="河南省郑州市金水区aa";

       String c=b.toUpperCase();

       System.out.println(a==b); // true

       System.out.println(a.equals(b));// true

       System.out.println(c.equals(b)); // false

       System.out.println(a.equalsIgnoreCase(c)); // true

    }

}

7.7.1 是否以子字符串开始或者结束

startsWith //以...开始

endsWith //以...结束

7.8 StringBuffer

StringBuffer是可变长的字符串

追加 删除 插入 反转

append追加 
delete 删除 
insert 插入 
reverse 反转

public class Test {

    public static void main(String[] args) {

       String a="河南省郑州市金水区";

       StringBuffer b=new StringBuffer(a);

       System.out.println(b.append("aa")); // 河南省郑州市金水区aa

       System.out.println(b.delete(6, 8)); // 河南省郑州市区aa 左闭右开 index从0开始

       System.out.println(b.insert(6, "金水区")); // 河南省郑州市金水区区aa

       System.out.println(b.reverse()); //aa区区水金市州郑省南河

    }

}

7.8.1 value和capacity

value:用于存放字符数组
capacity: 容量
无参构造方法: 根据容量初始化value

public MyStringBuffer(){

    value = new char[capacity];

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值