常用API---String,Math方法

1.1、String
创建字符串的常见3+1种方式。
三种构造方法:
public String():创建一个空白字符串,不含有任何内容。
public String(char[] array):根据字符数组的内容,来创建对应的字符串。
public String(byte[] array):根据字节数组的内容,来创建对应的字符串。
一种直接创建:
String str = "Hello"; // 右边直接用双引号

注意:直接写上双引号,就是字符串对象。
字符串常量池:程序当中直接写上的双引号字符串,就在字符串常量池中
 */
public class Demo01String {

    public static void main(String[] args) {
        // 使用空参构造
        String str1 = new String(); // 小括号留空,说明字符串什么内容都没有。
        System.out.println("第1个字符串:" + str1);

        // 根据字符数组创建字符串
        char[] charArray = { 'A', 'B', 'C' };
        String str2 = new String(charArray);
        System.out.println("第2个字符串:" + str2);

        // 根据字节数组创建字符串
        byte[] byteArray = { 97, 98, 99 };
        String str3 = new String(byteArray);
        System.out.println("第3个字符串:" + str3);

        // 直接创建
        String str4 = "Hello";
        System.out.println("第4个字符串:" + str4);
    }
}
1.2、常用的String方法
package demo03String;
/*
String 常用的获取方法:
1.pubulic int length(); 获取字符串长度
2.public String concat(String str); 连接两个字符串
3.public char charAt(int index); 获取指定索引位置的字符
4.public int indexOf(String str); 查找参考字符在本次字符串出现的第一次索引位置
5.public String Substring(int index);截取从index索引值开始到最后的字符串
6.public String Substring(int begin,int end);截取从begin索引值到索引值end-1的字符串
7.public char[] tocharArray(String str);把字符串转换成字符数组;
8.public byte[] getByte(String str);把字符串转换成字节数组;
9.public String replace(charSequence oldstring,charSequence newstring);替换字符串的字符
10.public Sting[] Split(String regex);按照指定参数将字符串分割成若干部分;
*/
public class demo01Stringget {
    public static void main(String[] args) {
        //1.获取字符串长度
        int length = "Jasonjones".length();
        System.out.println("请输出当前字符串长度"+length);
        //2.连接当前字符串与参数字符串
        String newname = "Hello-".concat("Jason");
        System.out.println("拼接后的字符串:"+newname);
        //3.获取指定索引位置
        char ch = newname.charAt(7);
        System.out.println(ch);
        //4.查找参考字符的第一次索引位置
        int llo = newname.indexOf("llo");
        System.out.println("llo的第一次索引位置:"+llo);//运行结果:llo的第一次索引位置:2
        //5.下面两种写法,字符串本身并没有改变,改变的是str1保存接收字符串的地址值
        String str1 = newname.substring(5);
        System.out.println(str1);//运行结果-Jason
        str1 = newname.substring(5, 7);
        System.out.println(str1);//运行结果-J
        //6.截取从begin索引值到索引值end-1的字符串
        String str2 ="HelloMike";
        System.out.println(str2.substring(5,9));//运行结果:Mike
        //7.把字符串转换成字符数组
        char[] chars = str2.toCharArray();
        System.out.println(chars.length);//运行结果:9(字符数组总长度)
        System.out.println(chars[1]);//运行结果:e
        //8.把字符串转换成字节数组;
        byte[] by = "abc".getBytes();
        System.out.println(by[0]);//97
        System.out.println(by[1]);//98
        System.out.println(by[2]);//99
        //9.替换字符串的字符
        String sss = "你玩的好坑啊,你大爷的,你大爷的,你大爷的".replace("你大爷的", "****");
        System.out.println(sss);//你玩的好坑啊,****,****,****
        //10.按照指定参数将字符串分割成若干部分
        String[] sp = "aaa,bbb,ccc".split(",");
        System.out.println(sp);
        System.out.println(sp[0]);//aaa
        System.out.println(sp[1]);//bbb
        System.out.println(sp[2]);//ccc
        //注意事项:Split是一个正则表达式,用英文句点切割时需要在切割符合前加\\
        String[] sp1 = "aaa.bbb.ccc".split(".");
        System.out.println(sp1[0]);
        System.out.println(sp1[1]);
    }
}
//参数数组打印出来的是数组的地址值,要获取数组内容需遍历数组或转换成字符数组在打印(个人总结)
2、 Math
package demo05Arrays;
//Math的常用方法
public class demo03 {
    public static void main(String[] args) {
        System.out.println(Math.abs(-3.14));//获取绝对值--->3.14
        System.out.println(Math.ceil(-3.14));//向上取整--->3.0
        System.out.println(Math.floor(-3.14));//向下取整--->4.0
        System.out.println(Math.round(-3.14));//四舍五入--->3
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值