java-String类用法

String类

String字符串常量特点

字符串是常量;它们的值在创建之后不能更改,这是什么意思呢?
就是说一旦这个字符串确定,那么就会在内存区域中生成这个字符串。
字符串本身不能改变,但str变量中记录的地址值是可以改变的。

String两种方式创建字符串的区别

String特点:一旦被赋值就不能改变
通过使用双引号的方式创建对象与new的方式创建对象,有什么不同呢?看如下程序:

String s3 = "abc";
String s4 = new String("abc");
System.out.println(s3==s4);//false
System.out.println(s3.equals(s4));//true,
//因为String重写了equals方法,建立了字符串自己的判断相同的依据(通过字符串对象中的字符来判断)

s3和s4的创建方式有什么不同呢?
s3创建,在内存中只有一个对象。这个对象在字符串常量池中
s4创建,在内存中有两个对象。一个new的对象在堆中,一个字符串本身对象,在字符串常量池中

String的构造方法

  • public String():空构造
  • public String(byte[] bytes):把字节数组转成字符串
  • public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
  • public String(char[] value):把字符数组转成字符串
  • public String(char[] value,int index,int count):把字符数组的一部分转成字符串,index第一位开始取(包括),count取多少个
  • public String(String original):把字符串常量值转成字符串
  • length() 返回此字符串的长度。

public String():空构造

class Test {
    public static void main(String[] args) {
    String s=new String();
        System.out.println(s);
    }
}
//无输出值,并不是null

如果要输出需要主动添加值

class Test {
    public static void main(String[] args) {
    String s=new String("aaa");
        System.out.println(s);
    }
}
//输出:aaa

public String(byte[] bytes):把字节数组转成字符串

注意是字符串

class Test {
    public static void main(String[] args) {
        //添加字节
        byte[] bytes = {'a', 'b', 'c', 'd'};
        String s =new String(bytes);
        System.out.println(s);
        //输出:abcd

        //添加数字
        //会根据Ascii码输出
        byte[] bytes2 = {97,98,99,100};
        String s1 = new String(bytes2);
        System.out.println(s1);
        //输出:abcd
    }
}

public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串

注意是字符串

class Test {
    public static void main(String[] args) {
		//public String(byte[] bytes,int index,int length)
		// index 起始位置,length 取多少个
		
        //添加字节
        byte[] bytes = {'a', 'b', 'c', 'd'};
        // 参数offset:为数组元素的起始索引位置,
		// 参数length为要几个元素
        String s =new String(bytes,1,2);
        System.out.println(s);
        //输出:bc

        //添加数字
        //会根据Ascii码输出
        byte[] bytes2 = {97,98,99,100};
        String s1 = new String(bytes2,1,2);
        System.out.println(s1);
        //输出:bc
    }
}

public String(char[] value):把字符数组转成字符串

注意是字符串

class Test {
    public static void main(String[] args) {
    	//添加字符
        char[] chars = {'a', 98, 'c', 100};
        String s = new String(chars);
        //根据Ascii码输出
        System.out.println(s);
        //输出:abcd
    }
}

public String(char[] value,int index,int count):把字符数组的一部分转成字符串,index第一位开始取(包括),count取多少个

注意是字符串

class Test {
    public static void main(String[] args) {
        // public String(char[] value,int index,int count)
        // index 起始位置,length 取多少个
        //添加字符
        char[] chars = {'a', 98, 'c', 100};
        // 参数offset为数组元素的起始索引位置
		// 参数count为要几个元素
        String s = new String(chars,1,2);
        //根据Ascii码输出
        System.out.println(s);
        //输出:bc
    }
}

public String(String original):把字符串常量值转成字符串

class Test {
    public static void main(String[] args) {
        String s=new String("我爱学习");
        System.out.println(s);
        //输出:我爱学习
    }
}

length() 返回此字符串的长度

class Test {
    public static void main(String[] args) {
        String s = "我爱学习";
        System.out.println(s.length());
        //输出:4
    }
}

String类的判断功能

  • boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  • boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  • boolean contains(String str):判断大字符串中是否包含小字符串,区分大小写
  • boolean startsWith(String str):判断字符串是否以某个指定的字符串开头,区分大小写
  • boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾,区分大小写
  • boolean isEmpty():判断字符串是否为空。

boolean equals(Object obj):比较字符串的内容是否相同,区分大小写

class Test {
    public static void main(String[] args) {
        String s1 = "ZhangSan";// Z/S大
        String s2 = "ZhangSan";// Z/S大
        String s3 = "zhangsan";// z/s小
        
        System.out.println(s1.equals(s2));//true
        System.out.println(s1.equals(s3));//false
    }
}

boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写

class Test {
    public static void main(String[] args) {
        String s1 = "ZhangSan";// Z/S大
        String s2 = "ZhangSan";// Z/S大
        String s3 = "zhangsan";// z/s小

        System.out.println(s1.equalsIgnoreCase(s2));//true
        System.out.println(s1.equalsIgnoreCase(s3));//true
    }
}

boolean contains(String str):判断大字符串中是否包含小字符串,区分大小写

class Test {
    public static void main(String[] args) {
        String s1 = "ZhangSan";// Z/S大
        String s2 = "ZhangSan";// Z/S大
        String s3 = "zhangsan";// z/s小

        System.out.println(s1.contains("ang"));//true
        System.out.println(s1.contains("Ang"));//false
    }
}

boolean startsWith(String str):判断字符串是否以某个指定的字符串开头,区分大小写

class Test {
    public static void main(String[] args) {
        String s1 = "ZhangSan";// Z/S大
        String s2 = "ZhangSan";// Z/S大
        String s3 = "zhangsan";// z/s小

        System.out.println(s1.startsWith("z"));//输入小写z,结果:false
        System.out.println(s1.startsWith("Z"));//输入大写Z,结果:true
    }
}

boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾,区分大小写

class Test {
    public static void main(String[] args) {
        String s1 = "ZhangSan";// Z/S大
        String s2 = "ZhangSan";// Z/S大
        String s3 = "zhangsan";// z/s小
        
        System.out.println(s1.endsWith("an"));//true
        System.out.println(s1.endsWith("An"));//false
    }
}

boolean isEmpty():判断字符串是否为空

class Test {
    public static void main(String[] args) {
        String s1 = "ZhangSan";// Z/S大
        String s2 = "ZhangSan";// Z/S大
        String s3 = "zhangsan";// z/s小

        System.out.println(s1.isEmpty());//false
    }
}

String类的获取功能

  • char charAt(int index):获取指定索引位置的字符
  • int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
    
  • int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
    
  • int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
    
  • int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
    
  • String substring(int start):从指定位置开始截取字符串,默认到末尾。
    
  • String substring(int start,int end):从指定位置开始到指定位置结束截取字符串,不包含结束位置
    

char charAt(int index):获取指定索引位置的字符

class Test {
    public static void main(String[] args) {
        String s = "abcdefg";
        System.out.println(s.charAt(5));
        //输出:f
    }
}

int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引

class Test {
    public static void main(String[] args) {
        String s = "abcdefg";
        System.out.println(s.indexOf("d"));
        //输出:3
    }
}

int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引

class Test {
    public static void main(String[] args) {
        String s = "abcdefg";
        System.out.println(s.indexOf("cd"));
        //输出:2
    }
}

int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。

class Test {
    public static void main(String[] args) {
        String s = "abccba";
        System.out.println(s.indexOf("c"));
        //输出:2
    }
}

int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

class Test {
    public static void main(String[] args) {
        String s = "abccbaabccba";
        System.out.println(s.indexOf("cb"));
        //输出:3
    }
}

String substring(int start):从指定位置开始截取字符串,默认到末尾。

class Test {
    public static void main(String[] args) {
        String s = "abcdefg";
        System.out.println(s.substring(5));
        //输出:fg
    }
}

String substring(int start,int end):从指定位置开始到指定位置结束截取字符串,不包含结束位置

class Test {
    public static void main(String[] args) {
        String s = "abcdefg";
        System.out.println(s.substring(2,5));
        //输出:cde
    }
}

String类的转换功能

  • byte[] getBytes():把字符串转换为字节数组。
  • char[] toCharArray():把字符串转换为字符数组。
  • static String valueOf(char[] chs):把字符数组转成字符串。
  • static String valueOf(int i):把int类型的数据转成字符串。
  • String toLowerCase():把字符串转成小写。
  • String toUpperCase():把字符串转成大写。
  • String concat(String str):把字符串拼接。

byte[] getBytes():把字符串转换为字节数组。

会有Ascii码转换

class Test {
    public static void main(String[] args) {
        String s = "abcd";
        byte[] bytes = s.getBytes();
        //遍历数组
        for (int i = 0; i < bytes.length; i++) {
        	//逐个取值,按照Ascii码
            System.out.println(bytes[i]);
            //输出:97
            //     98
            //     99
            //     100
        }
    }
}

char[] toCharArray():把字符串转换为字符数组。

class Test {
    public static void main(String[] args) {
        String s = "abcd";
        char[] chars = s.toCharArray();
        //遍历数组
        for (int i = 0; i < chars.length; i++) {
        //逐个取值
            System.out.println(chars[i]);
            //输出:a
            //     b
            //     c
            //     d
        }
    }
}

static String valueOf(char[] chs):把字符数组转成字符串。

class Test {
    public static void main(String[] args) {
        char[] chars = {'a', 'b', 'c', 'd'};
        System.out.println(String.valueOf(chars));
        //输出:abcd
        
        char[] chars2 = {97, 98, 99, 100};
        //Ascii码转
        System.out.println(String.valueOf(chars2));
        //输出:abcd
    }
}

static String valueOf(int i):把int类型的数据转成字符串。

class Test {
    public static void main(String[] args) {
        String s=String.valueOf(123456);
        System.out.println(s);
        //输出:123456
        //注意,此时的123456是一个s所代表的字符串
    }
}

String toLowerCase():把字符串转成小写

class Test {
    public static void main(String[] args) {
        String s="ABCD";
        System.out.println(s.toLowerCase());
        //输出:abcd
    }
}

String toUpperCase():把字符串转成大写。

class Test {
    public static void main(String[] args) {
        String s="abcd";
        System.out.println(s.toUpperCase());
        //输出:ABCD
    }
}

String concat(String str):把字符串拼接。

class Test {
    public static void main(String[] args) {
        String s = "ab";
        System.out.println(s.concat("cd"));
        //输出:abcd
    }
}

String替换功能

  • String replace(char old,char new):把字符串中的某个字符用新的字符所替换
  • String replace(String old,String new)把字符串中的某个字符串用新的来替换
  • String trim() 去除字符串两端空格

String replace(char old,char new):把字符串中的某个字符用新的字符所替换

class Test {
    public static void main(String[] args) {
        String s = "ab";
        System.out.println(s.replace("a", "c"));
        //输出:cb
    }
}

String replace(String old,String new)把字符串中的某个字符串用新的来替换

class Test {
    public static void main(String[] args) {
        String s = "ab";
        System.out.println(s.replace("ab", "cd"));
        //输出:cd
    }
}

String trim() 去除字符串两端空格

class Test {
    public static void main(String[] args) {
        String s = "     May all the beauty be blessed";
        System.out.println(s.trim());
        System.out.println(s);
        //输出:May all the beauty be blessed
        //          May all the beauty be blessed
   
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值