Java常用类(二)

目录

一、基本数据类型包装类

二、String类

1.两种创建形式:

1.String s = "abc"

2.String s = new String("abc")

2.构造方法

1.public String()

2.public String(String str)

3.public String(byte[] bytes)

4.public String(char[] value)

3.判断功能

1.boolean equals(Object obj)

2.boolean equalsIgnoreCase(String str)

3.boolean contains(String str)

4.boolean isEmpty()

5.boolean startsWith(String prefix)

6.boolean endsWith(String suffix)

4.获取功能

1.int length()

2.char charAt(int index)

3.int indexOf(String str)

4.int indexOf(String str,int fromIndex)

5.String substring(int start)

6.String substring(int start,int end)

5.转换功能

1.byte[] getBytes()

2.char[] toCharArray()

3.static String valueOf(char[] chs)

4.String toLowerCase()

5.String toUpperCase()

6.String concat(String str)

7.Stirng[] split(分割符);

6.替换功能

1.String replace(char old,char new)

2.String replace(String old,String new)

3.replaceAll(String regex, String replacement)

4.replaceFirst(String regex, String replacemen

7.去除字符串两空格

String trim()

三、Math类

1.abs 绝对值

2.sqrt 平方根

3.pow(double a, double b) a的b次幂

4.max(double a, double b)

5.min(double a, double b)

6.random() 返回 0.0 到 1.0 的随机数

7.long round(double a) double型的数据a转换为long型(四舍五入)

六、Random类

1.构造方法 public Random()

2.成员方法 public int nextInt()

public int nextInt(int n)


一、基本数据类型包装类

为了对基本类型更加方便的操作

public class MyInt {
 
    int value;
 
    public MyInt(int value) {
        this.value = value;
    }
 
    public int maxint() {
        return Integer.MAX_VALUE;
    }
 
    public int minint() {
        return Integer.MIN_VALUE;
    }
 
    public static void main(String[] args) {
        int a = 10;
        MyInt b = new MyInt(a);
    }
 
 
}
public class MyInt {
 
    int value;
 
    public MyInt(int value) {
        this.value = value;
    }
 
    public int maxint() {
        return Integer.MAX_VALUE;
    }
 
    public int minint() {
        return Integer.MIN_VALUE;
    }
 
    public static void main(String[] args) {
        int a = 10;
        MyInt b = new MyInt(a);
    }
 
 
}

二、String类

1.两种创建形式:

1.String s = "abc"

2.String s = new String("abc")

String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1 == s2); //true
        System.out.println(s1.equals(s2));//true

        String s3=new String("abc");
        String s4=new String("abc");
        System.out.println(s3==s4);//false
        System.out.println(s3.equals(s4));//true

2.构造方法

1.public String()

2.public String(String str)

3.public String(byte[] bytes)

4.public String(char[] value)

 public static void main(String[] args) throws UnsupportedEncodingException {
        String s1 = new String(); // this.value = "".value;
        String s2 = new String("abc");
        String s3 = "abc你好";

//        byte[] bytes = s3.getBytes();//把字符串转为byte类型  转码
        byte[] bytes = s3.getBytes("utf-8");//把字符串转为byte类型  转码
        System.out.println(Arrays.toString(bytes));//打印数组

//        String s4 = new String(bytes);//  解码
        String s4 = new String(bytes, "gbk");//解码
        System.out.println(s4);

        String s5 = "cbad";
        char chars[] = s5.toCharArray();//字符串 --> char数组
        Arrays.sort(chars);//将字符串进行排序
        String s6 = new String(chars);//char数组 -->字符串
        System.out.println(s6);

    }

3.判断功能

1.boolean equals(Object obj)

2.boolean equalsIgnoreCase(String str)

3.boolean contains(String str)

4.boolean isEmpty()

5.boolean startsWith(String prefix)

6.boolean endsWith(String suffix)

String s = "abcd";
        System.out.println(s.equals("abcD"));//s和另一个字符串比较,是否相等
        System.out.println(s.equalsIgnoreCase("abcD"));//忽略大小写
        System.out.println(s.contains("a"));
        System.out.println(s.contains("ab"));//判断是否包含子串(子串必须是连续的)
        System.out.println(s.contains("ac"));//false  判断时子串只能是连续的
        System.out.println(s.isEmpty());//s="";   判断是否是空串  并非是null
        System.out.println(s.startsWith("ab"));//判断是否以指定的子串开头
        System.out.println(s.endsWith("cd"));//判断是否以指定的子串结尾

4.获取功能

1.int length()

2.char charAt(int index)

3.int indexOf(String str)

4.int indexOf(String str,int fromIndex)

5.String substring(int start)

6.String substring(int start,int end)

 String s = "abcdcde";
        System.out.println(s.length());//求字符串长度
        System.out.println(s.charAt(2));//索引得到第几个元素
        System.out.println(s.indexOf("c"));//返回指定字符首次出现的位置
        System.out.println(s.indexOf("cd"));//返回指定字符串首字母首次出现的位置
        System.out.println(s.indexOf("c", s.indexOf("c") + 1));//从指定的位置开始查找
        System.out.println(s.lastIndexOf("c"));//从后向前查找

        String s1 = s.substring(3);//从指定的位置开始裁取字符串,返回一个新的子字符串
        String s2 = s.substring(0,4);//从指定的位置开始到指定的位置结束(不包含结束)截取字符串,返回一个新的子字符串
        System.out.println(s1);
        System.out.println(s2);

5.转换功能

1.byte[] getBytes()

2.char[] toCharArray()

3.static String valueOf(char[] chs)

4.String toLowerCase()

5.String toUpperCase()

6.String concat(String str)

7.Stirng[] split(分割符);

Integer a = 10;
//        System.out.println(a.toString());
        String s = String.valueOf(a);//把其他类型转为字符串 建议使用的,避免出现空指针异常
        System.out.println(s);

        char[] c = {'a', 'b', 'c' };
        String s1 = String.valueOf(c);

        String s2 = "abcDEF你好";
        System.out.println(s2.toLowerCase());//英文全部转为小写  汉字无关
        System.out.println(s2.toUpperCase());//英文全部转为大写

        String s3 = s2.concat("wwww");//将指定字符串拼接到字符串末尾,返回一个新的字符串
        System.out.println(s3);

        String s4 = "ab;cde;fg";
        String[]strings=s4.split(";");//使用指定的字符将字符串分割为数组   正则表达式
        System.out.println(Arrays.toString(strings));

6.替换功能

1.String replace(char old,char new)

2.String replace(String old,String new)

3.replaceAll(String regex, String replacement)

4.replaceFirst(String regex, String replacemen

String s = " abccd efg ";
        System.out.println(s.replace('c', 'C'));//字符替换
        System.out.println(s.replace("cd", "dc"));//字符串替换

        // replaceALL() 使用正则表达匹配需要替换的内容
        System.out.println(s.replaceAll("c", "CC"));
        System.out.println(s.replaceFirst("c", "CC"));//只替换第一个   没有只替换最后一个

7.去除字符串两空格

String trim()

String s = " abccd efg ";
System.out.println(s.length());//求字符串长度
        System.out.println(s.trim().length());//去掉前面和后面的空格
        System.out.println(s.replace(" ", "").length());//将空格替换

三、Math类

1.abs 绝对值

2.sqrt 平方根

3.pow(double a, double b) a的b次幂

4.max(double a, double b)

5.min(double a, double b)

6.random() 返回 0.0 到 1.0 的随机数

7.long round(double a) double型的数据a转换为long型(四舍五入)

 System.out.println(Math.PI);
        System.out.println(Math.abs(-1));
        System.out.println(Math.sqrt(9));
        System.out.println(Math.pow(2, 3));

        System.out.println(Math.floor(9.9));
        System.out.println(Math.ceil(9.1));

        System.out.println(Math.round(9.4));
        System.out.println(Math.round(9.6));

        System.out.println(Math.random());//返回一个大于等于0 小于1的一个随机数

六、Random类

此类用于产生随机数

1.构造方法 public Random()

2.成员方法 public int nextInt()

public int nextInt(int n)

Random random = new Random();
        System.out.println(random.nextBoolean());
        System.out.println(random.nextDouble());
        System.out.println(random.nextFloat());
        System.out.println(random.nextInt());//在int取值范围内随机返回一个结果
        System.out.println(random.nextInt(10));//在指定的范围内返回一个随机数 大于等于0 小于给定的值

        byte[] bytes = new byte[5];
        random.nextBytes(bytes);
        System.out.println(Arrays.toString(bytes));

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘伊珂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值