Java String类、StringBuffer类及常用方法

Java String类及常用方法

一、概述
  • String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。字符串是常量,一旦被创建,就不能被改变

  • 字符串是由多个字符组成的一串数据(字符序列),字符串可以看成是字符数组

二、常见构造方法
  • public String():空构造,表示一个空字符序列

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

  • public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)

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

  • public String(char[] value,int index,int count):把字符数组的一部分转成字符串

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

    示例

	public static void main(String[] args) {
        String s = new String();

        byte[] bytes = {97,98,99};
        String s1 = new String(bytes); //s1="abc"

        String s2 = new String(bytes,0,2); //s2 = "ab"

        char[] chars = {'a','b','c'};
        String s3 = new String(chars); //s3 = "abc"

        String s4 = new String(chars,0,2); //s4 = "ab"

        String s5 = new String("abcd"); //s5 = "abcd"
    }
三、String类的判断功能
  • public boolean equals(Object obj) 将此字符串与指定对象比较,区分大小写
  • public boolean equalsIgnoreCase(String str) 比较字符串的内容是否相同,忽略大小写
  • public boolean contains(String str) 判断字符串中是否包含传递进来的字符串
  • public boolean startsWith(String str) 判断字符串是否以传递进来的字符串开头
  • public boolean endsWith(String str) 判断字符串是否以传递进来的字符串结尾
  • public boolean isEmpty() 判断字符串的内容是否为空串

示例

    public static void main(String[] args) {
        String s1 = "abcde";
        String s2 = "ccccc";
        boolean b = s1.equals(s2); //b = false

        boolean b1 = s1.equalsIgnoreCase("ABCDE"); //true

        boolean b2 = s1.contains("abc"); //true

        boolean b3 = s1.startsWith("a"); //true

        boolean b4 = s1.endsWith("de"); //true

        boolean b5 = s1.isEmpty();//false
    }
四、String类的转换功能
  • public byte[] getBytes(): 把字符串转换为字节数组
  • public char[] toCharArray(): 把字符串转换为字符数组。
  • public static String valueOf(char[] chs): 把字符数组转成字符串
  • public static String valueOf(int i): 把int类型的数据转成字符串
  • public String toLowerCase(): 把字符串转成小写
  • public String toUpperCase(): 把字符串转成大写
  • public String concat(String str): 把字符串拼接
public static void main(String[] args) {
        String s = "abcde";
        byte[] bytes = s.getBytes(); //97 98 99 100 101

        char[] chars = s.toCharArray(); //a b c d e

        String s1 = String.valueOf(chars);

        String s2 = String.valueOf(97); //s2 = "97"

        String s3 = "asdSFSFD";
        String s4 = s3.toLowerCase(); //s4 = "asdsfsfd"
        String s5 = s3.toUpperCase(); //s5 = "ASDSFSFD"

        String s6 = s4.concat(s5);// s6 = "asdsfsfdASDSFSFD"
    }
五、String类的其他功能
  • public String replace(char old,char new) 将指定字符进行互换
  • public String replace(String old,String new) 将指定字符串进行互换
  • public String trim() 去除两端空格
  • public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
  • public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
    public static void main(String[] args) {
        String s = "abcde";
        String s1 = s.replace("ab", "**");// s1 = "**cde"
        String s2 = s.replace('a', '*');// s2 = "*bcde"

        String s3 = "   abc    ";
        String s4 = s3.trim();//s4 = "abc"

        String s5 = "bcd";
        int i1 = s5.compareTo(s4);// i1 = 1 即98-97
        int i = s.compareTo(s4);// i = 2 长度不一样返回长度之差
    }
六、String的特点及面试题
  • 特点

    一旦被创建就不能改变,因为字符串的值是在方法区的常量池中划分空间,分配地址值

  • 案例

    String s = “hello” ;
    s = “world” + “java”;

    System.out.println(s);//输出:worldjava

  • 面试题1

    String s = new String(“hello”)和String s = “hello”;的区别,并画内存图解释

    String s = new String(“hello”)首先在堆中创建一块内存,内存地址返回给栈内存的s,然后因为"hello"是常量,jvm会去方法区中的字符串常量池查看是否有"hello"字符串对象,没有的话就会分配一个空间来存放"hello",并将其空间地址存入堆中new出来的对象中

    String s = “hello”首先jvm会直接检查字符串常量池是否已经有了"hello"字符串对象,如果有,就会分配一个对象存放"hello",如果有,就直接将字符串常量池中的地址返回给栈内存中的s而不通过堆中new的对象
    在这里插入图片描述

  • 面试题2

    == 和 equals() 的区别

    ==:基本类型比较的是值,引用类型比较地址值

    equals():只能比较引用数据类型,Object类中与==无任何区别,重写后可比较对象属性

  • 面试题3

    看程序写结果

    	String s1 = new String("hello");
    	String s2 = new String("hello");
    	System.out.println(s1 == s2);//false
    	System.out.println(s1.equals(s2));//true
    
    	String s3 = new String("hello");
    	String s4 = "hello";
    	System.out.println(s3 == s4);//false
    	System.out.println(s3.equals(s4));//true
    
    	String s5 = "hello";
    	String s6 = "hello";
    	System.out.println(s5 == s6);//true
    	System.out.println(s5.equals(s6));//true
    
七、在一个字符串中查找另一个字符串出现的次数
public class StringDemo7 {
    public static void main(String[] args) {
        String maxStr = "woaijavawozjavahendeaijavawohenajavaihenaijava";
        String minStr = "java";
        int count = 0;
        while (maxStr.indexOf(minStr)!=-1){
            count++;
            //从需要查找的字符串的索引加小串的长度截取,得到需被查找的字符串第一次出现之后的字符串
            maxStr = maxStr.substring(maxStr.indexOf(minStr)+minStr.length());
        }
        System.out.println(count); //5
    }
}
八、StringBuffer
  1. 概述

对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题

  1. 构造方法

    public StringBuffer():				无参构造方法
    public StringBuffer(int capacity):	指定容量的字符串缓冲区对象
    public StringBuffer(String str):		指定字符串内容的字符串缓冲区对象
    
  2. StringBuffer的获取长度及容量方法

    • public int capacity():返回当前容量。 理论值
    • public int length():返回长度(字符数)。 实际值
  3. StringBuffer的添加功能

    • public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
    • public StringBuffer append(String str) 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
  4. StringBuffer的删除功能

    • public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
    • public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
  5. StringBuffer的替换和反转功能

    • public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
    • public StringBuffer reverse(): 字符串反转
  6. StringBuffer的截取功能及注意事项

    • public String substring(int start): 从指定位置截取到末尾

    • public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置

      注意:返回值类型不再是StringBuffer本身

  7. StringBuffer和String的相互转换

    • String – StringBuffer

      a:通过构造方法
      b:通过append()方法

    • StringBuffer – String
      a: 使用substring方法
      b:通过构造方法
      c:通过toString()方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值