String类

一、String类
引用数据类型,表示一个字符序列,比如:字符串字面量“ABC”表示这个类的一个实例,一般存在常量池中。
特点:
字符串一旦创建不可变
数据通过私有的一个char[]来存储字符序列,并且String类中没有提供对外的方法来操作此属性,所以不可变。
字符串常量存储在常量池中,并且只有一份
创建字符串对象的方式:
String s = “abc”;

            通过构造方法:
            new String(); //不推荐
            new String("abc");//如果常量池中没有“abc”,则先在常量池存储,然后再在堆中new一个String对象。创建俩个对象。不推荐
            new String(char[] arr);
            new String(char[] arr,int offset,int count)
            new String(byte[] arr);
            new String(byte[] arr,int offset,int count)

            通过连接符号+,字符串与任意类型使用+连接,结果为字符串
    字符串常用方法:
            (1)boolean isEmpty():字符串是否为空 //String s = ""; String s = " "; String s  = null;
            (2)int length():返回字符串的长度
            (3)String concat(xx):拼接,等价于+
            (4)boolean equals(Object obj):比较字符串是否相等,区分大小写
            (5)boolean equalsIgnoreCase(Object obj):比较字符串是否相等,不区分大小写
            (6)int compareTo(String other):比较字符串大小,区分大小写,按照Unicode编码值比较大小
            (7)int compareToIgnoreCase(String other):比较字符串大小,不区分大小写
            (8)String toLowerCase():将字符串中大写字母转为小写
            (9)String toUpperCase():将字符串中小写字母转为大写
            (10)String trim():去掉字符串前后空白符
            (11)boolean contains(xx):是否包含xx
            (12)int indexOf(xx):从前往后找当前字符串中xx,即如果有返回第一次出现的下标,要是没有返回-1
            (13)int lastIndexOf(xx):从后往前找当前字符串中xx,即如果有返回最后一次出现的下标,要是没有返回-1
            (14)String substring(int beginIndex) :返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。
            (15)String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
            (16)char charAt(index):返回[index]位置的字符
            (17)char[] toCharArray(): 将此字符串转换为一个新的字符数组返回
            (18)String(char[] value):返回指定数组中表示该字符序列的 String。
            (19)String(char[] value, int offset, int count):返回指定数组中表示该字符序列的 String。
            (20)static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的 String
            (21)static String copyValueOf(char[] data, int offset, int count):返回指定数组中表示该字符序列的 String
            (22)static String valueOf(char[] data, int offset, int count) : 返回指定数组中表示该字符序列的String
            (23)static String valueOf(char[] data) :返回指定数组中表示该字符序列的 String
            (24)byte[] getBytes():编码,把字符串变为字节数组,按照平台默认的字符编码进行编码byte[] getBytes(字符编码方式):按照指定的编码方式进行编码
            (25)new String(byte[] ) 或 new String(byte[], int, int):解码,按照平台默认的字符编码进行解码new String(byte[],字符编码方式 ) 或 new String(byte[], int, int,字符编码方式):解码,按照指定的编码方式进行解码
            (26)boolean startsWith(xx):是否以xx开头
            (27)boolean endsWith(xx):是否以xx结尾
            (29)String replace(xx,xx):不支持正则
            (30)String replaceFirst(正则,value):替换第一个匹配部分
            (31)String repalceAll(正则, value):替换所有匹配部分
            (32)String[] split(正则):按照某种规则进行拆分
    内存分析:
            String s1 = "abc";
            String s2 = "abc";
            String s3 = new String("abc");//堆中开辟新的内存空间
            String s4 = "a" + "bc"; //编译器优化为"abc"
            final String s5 = "a";
            String s6 = "bc";
            String s7 = s5 + s6;//堆中创建了新的对象
            String s8 = s5 + "bc";//常量+常量 等同于s4

二、可变长度的字符串序列,拼接字符串时效率高于直接操作String对象。
StringBuilder:线程不安全的
StringBuffer:线程安全的

    使用方法:
            StringBuilder sb = new StringBuilder("abc");//构造方法
            sb.append("efg");//尾部拼接字符串
            sb.append("hij");
            String s = sb.toString();//转为字符串对象

三、数学相关类
1.Math类
提供了一些基础的数学运算方法:
public static final double PI:返回圆周率
public static double abs(double a) :返回 double 值的绝对值。
public static double ceil(double a) :返回大于等于参数的最小的整数。
public static double floor(double a) :返回小于等于参数最大的整数。
public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
public static double pow(double a,double b):返回a的b幂次方法
public static double sqrt(double a):返回a的平方根
public static double random():返回[0,1)的随机值
public static double max(double x, double y):返回x,y中的最大值
public static double min(double x, double y):返回x,y中的最小值
2.Random类
构造方法:
Random r = new Random();
Random r = new Random(int seed);//需要种子
常用方法:
int nextInt();//返回一个伪随机整数
int nextInt(int a);//返回一个[0,a)伪随机整数
3.BigInteger类:用于精确计算超大整数
BigInteger(String val)
BigInteger add(BigInteger val)
BigInteger subtract(BigInteger val)
BigInteger multiply(BigInteger val)
BigInteger divide(BigInteger val)
BigInteger remainder(BigInteger val)
int intValue():将此 BigInteger 转换为 int。
long longValue():将此 BigInteger 转换为 long。
float floatValue():将此 BigInteger 转换为 float。
4.BigDecimal:用于精确计算任意大小的小数
BigDecimal(String val)
BigDecimal add(BigDecimal val)
BigDecimal subtract(BigDecimal val)
BigDecimal multiply(BigDecimal val)
BigDecimal divide(BigDecimal val)
BigDecimal divide(BigDecimal divisor, int roundingMode)
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
BigDecimal remainder(BigDecimal val)
double doubleValue():将此 BigDecimal 转换为 double。

/*
(1)boolean isEmpty():字符串是否为空
(2)int length():返回字符串的长度
(3)String concat(xx):拼接,等价于+
(4)boolean equals(Object obj):比较字符串是否相等,区分大小写
(5)boolean equalsIgnoreCase(Object obj):比较字符串是否相等,区分大小写
(6)int compareTo(String other):比较字符串大小,区分大小写,按照Unicode编码值比较大小
(7)int compareToIgnoreCase(String other):比较字符串大小,不区分大小写
(8)String toLowerCase():将字符串中大写字母转为小写
(9)String toUpperCase():将字符串中小写字母转为大写
(10)String trim():去掉字符串前后空白符
 */
package com.panda.test;

public class Demo {
    public static void main(String[] args) {
        String s = "ABC";
        String s1 = "";
       // System.out.println(s1.isEmpty());
        // System.out.println(s.length());

        String s2 = "D";
       // System.out.println(s.concat(s2));
        /*
(4)boolean equals(Object obj):比较字符串是否相等,区分大小写
(5)boolean equalsIgnoreCase(Object obj):比较字符串是否相等,区分大小写
         */
        String s3 = "aaa";
        String s4 = "aaa";
        String s5 = "Aaa";
        String s6 = "bbb";
        System.out.println(s3.equals(s4));
        System.out.println(s3.equals(s5));
        System.out.println(s3.equals(s6));

        System.out.println(s3.equalsIgnoreCase(s5));

        /*
(8)String toLowerCase():将字符串中大写字母转为小写
(9)String toUpperCase():将字符串中小写字母转为大写
(10)String trim():去掉字符串前后空白符
         */
        System.out.println(s6.toUpperCase());
        String ss = "          asdsas              ";
        System.out.println(ss.trim());


    }
}

/*
public String() :初始化新创建的 String对象,以使其表示空字符序列。
String(String original) : 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
public String(char[] value,int offset, int count) :通过字符数组的一部分来构造新的String。
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
public String(byte[] bytes,String charsetName) :通过使用指定的字符集解码当前参数中的字节数组来构造新的String。
 */
package com.panda.test;

public class Demo2 {
    public static void main(String[] args) {
        String s1 = new String("dsad");
        System.out.println(s1);
        String s2 = new String(s1);
        System.out.println(s2);
        char[] ch = {'a','d','w'};
        String s3 = new String(ch);
        System.out.println(s3);
        String s4 = new String(ch,0,3);
        System.out.println(s4);

        byte[] bytes = {23,4,43,97};
        String s5 = new String(bytes);
        System.out.println(s5);

    }
}

/*
(11)boolean contains(xx):是否包含xx
(12)int indexOf(xx):从前往后找当前字符串中xx,即如果有返回第一次出现的下标,要是没有返
回-1
(13)int lastIndexOf(xx):从后往前找当前字符串中xx,即如果有返回最后一次出现的下标,要是没
有返回-1

 */
package com.panda.test;

public class Demo3 {
    public static void main(String[] args) {
        String s1 = "dashjfhsjakdjwirs";
        String s2 = "aks";
        System.out.println(s1.contains(s2));

        String s3 = "a";
        System.out.println(s1.indexOf(s3));
        String s4 = "al";
        System.out.println(s1.lastIndexOf(s4));
        /*
(14)String substring(int beginIndex) :返回一个新的字符串,它是此字符串的从beginIndex开始截
取到最后的一个子字符串。
(15)String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从
beginIndex开始截取到endIndex(不包含)的一个子字符串。
 */
        System.out.println(s1.substring(3));
        System.out.println(s1.substring(3, 9));
/*
 (16)char charAt(index):返回[index]位置的字符
(17)char[] toCharArray(): 将此字符串转换为一个新的字符数组返回
(18)String(char[] value):返回指定数组中表示该字符序列的 String。
(19)String(char[] value, int offset, int count):返回指定数组中表示该字符序列的 String。
(20)static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的 String
(21)static String copyValueOf(char[] data, int offset, int count):返回指定数组中表示该字符序列
的 String
(22)static String valueOf(char[] data, int offset, int count) : 返回指定数组中表示该字符序列的
String
(23)static String valueOf(char[] data) :返回指定数组中表示该字符序列的 String
 */
        String ss1 = "sdsa sadd";
        System.out.println(ss1.charAt(3));
        System.out.println(ss1.toCharArray());

        char[] ch = {'a', 'b', 'c', 'd', 'e'};
        String ss2 = new String(ch, 0, 2);
        System.out.println(ss2);

        String ss3 = String.copyValueOf(ch);
        System.out.println(ss3);
        String ss4 = String.copyValueOf(ch,1,3);
        System.out.println(ss4);

        String ss5 = String.valueOf(2134);
        System.out.println(ss5);

        String ss6 = String.valueOf(ch,1,3);
        System.out.println(ss6);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值