String类与StringBuffer类

String类
String类的转换功能
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
example:

public class StringDemo10 {
    public static void main(String[] args) {
        //将字符串转换为字节数组
        //byte[] getBytes()
        String s = "BigData";
        byte[] bytes = s.getBytes();
        System.out.println(bytes);//打印的只是地址,想要打印内容,需要遍历出来
        for(int i=0;i<bytes.length;i++){
            System.out.println(bytes[i]);//打印出来的是ASCII码
        }
        System.out.println("****************************************");

        //将字符串转换为字符数组
        //char[] toCharArray()
        char[] chars = s.toCharArray();
        System.out.println(chars);
        for(int i=0;i<chars.length;i++){
            System.out.println(chars[i]);
        }
        System.out.println("****************************************");

        //将字符数组转换为字符串
        //static String valueOf(char[] chs)
        String s1 = String.valueOf(chars);
        System.out.println(s1);
        System.out.println("****************************************");

        //将int类型的数据转换成字符串
        //static String valueOf(int i)
        String s2 = String.valueOf(100);
        System.out.println(s2);
        System.out.println("****************************************");

        //将字符串中的字符都转成小写
        String s3 = s.toLowerCase();
        System.out.println(s3);
        System.out.println("****************************************");

        //将字符串中的字符都转成大写
        String s4 = s.toUpperCase();
        System.out.println(s4);
        System.out.println("****************************************");

        //拼接字符串
        //String concat(String str)
        String s5 = "hello";
        String s6 = "world";
        String s7 = s5 + s6;
        String s8 = s5.concat(s6);
        System.out.println("s5:"+s5);
        System.out.println("s6:"+s6);
        System.out.println("s7:"+s7);
        System.out.println("s8:"+s8);

    }
}
//[B@28d93b30
//66
//105
//103
//68
//97
//116
//97
//****************************************
//BigData
//B
//i
//g
//D
//a
//t
//a
//****************************************
//BigData
//****************************************
//100
//****************************************
//bigdata
//****************************************
//BIGDATA
//****************************************
//s5:hello
//s6:world
//s7:helloworld
//s8:helloworld

String类的其他功能

替换功能

  • String replace(char old,char new)
  • String replace(String old,String new)
String s = "HelloWorld";
        String replace = s.replace("l", "m");
        System.out.println(replace);//HemmoWormd
        String replace1 = s.replace("llo", "ppl");
        System.out.println(replace1);//HepplWorld

去除字符串两空格

  • String trim()
String s1 = "  HelloWorld   ";
        String trim = s1.trim();
        System.out.println(trim);//HelloWorld

按字典顺序比较两个字符串

  • int compareTo(String str)
  • int compareToIgnoreCase(String str)
String s5 = "hello";//h的ASCII码104
        String s6 = "hello";
        String s7 = "abc";//a的ASCII码97
        String s8 = "qwe";//q的ASCII码113
        String s9 = "hel";
        System.out.println(s5.compareTo(s6));   //两个相等,相减为0
        System.out.println(s5.compareTo(s7));   //h的ASCII码和a的ASCII码两个相减,结果是7
        System.out.println(s5.compareTo(s8));   //h的ASCII码和q的ASCII码两个相减,结果是-9
        System.out.println(s5.compareTo(s9));   //前面的都比较完了,剩下几个字符,结果就是几

统计大串中小串出现的次数

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入大串");
        String maxString = sc.next();
        System.out.println("请输入小串");
        String minString = sc.next();

        int result = findString(maxString,minString);
        System.out.println("大串中小串出现的次数为:"+result);
    }

    public static int findString(String maxString,String minString){
        //定义出现的次数
        int count = 0;
        //大串中小串的位置
        int index = maxString.indexOf(minString);

        if(index == -1){
            System.out.println("大串中没有这个小串");
            return 0;
        }

        while (index != -1){
            count ++;
            //被截取的长度
            int startindex = index + minString.length();
            maxString = maxString.substring(startindex);
            index = maxString.indexOf(minString);
        }
        return count;
    }

StringBuffer类
StringBuffer:
线程安全,可变的字符序列。字符串缓冲区就像一个String,但可以修改
在任何时间点,它包含一些特定的字符序列
但可以通过某些方法调用来更改序列的长度和内容
简单来说:就是一个线程安全的可变字符串
StringBuffer和String的区别
前者长度和内容都可以改变,后者不可改变
前者可以提前给出缓冲区,可以进行字符串拼接不会重新开辟空间;后者会开辟新的空间,会浪费太多资源
StringBuffer的构造方法
public StringBuffer()
public StringBuffer(int capacity)
public StringBuffer(String str)
StringBuffer的方法
public int capacity()返回当前容量(容量是新插入字符可用的存储量,超过此值将进行重新分配)
public int length()返回长度(字符数)
线程安全简单描述:
线程是安全的;安全,代表着有同步存在,数据是安全的,效率不高
不安全,不同步,效率高
安全问题和效率问题,一直都是一个矛盾点
安全:售票、取号、银行业务(都是一个一个进行的)
效率:论坛(可以同步进行的)
StringBuffer类的成员方法
添加功能
public StringBuffer append(String str)
可以把任意类型的数据添加到字符串缓冲区中,并且返回的是字符串缓冲区本身
public StringBuffer insert(int offset,String str)
在指定位置插入数据到字符串缓冲区
 

public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        StringBuffer sb2 = sb.append("hello");//append将任意类型数据添加到字符串缓冲区,返回的是字符串缓冲区本身

        System.out.println(sb);
        System.out.println(sb2);
        System.out.println(sb == sb2);//true


        sb.append(true);
        sb.append("world");
        sb.append(123);
        //这两个返回的是同一个内容,因为返回的都是字符串缓冲区本身
        System.out.println(sb);//hellotrueworld123
        System.out.println(sb2);//hellotrueworld123

        sb.append(12.34f).append("java").append(23).append(false);
        System.out.println(sb);
        System.out.println(sb2);
    }
}
//hello

删除功能

  • public StringBuffer deleteCharAt(int index)
  • public StringBuffer delete(int start,int end)
public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append(true).append(12).append(12.34f).append("hello");
        System.out.println(sb);
        System.out.println("**********************************");

        //删除指定范围的字符(左闭右开)
        sb.delete(0,4);
        System.out.println(sb);
        System.out.println("**********************************");

        //删除指定位置字符
        sb.deleteCharAt(2);
        System.out.println(sb);
        System.out.println("**********************************");

        //删除所有字符串
        sb.delete(0,sb.length());
        System.out.println(sb);
        System.out.println("**********************************");
    }

替换功能

  • public StringBuffer replace(int start,int end,String str)
public static void main(String[] args) {

        StringBuffer sb = new StringBuffer();
        sb.append(12).append(true).append("hello").append(34.56f);
        System.out.println(sb);
        System.out.println("*********************************");

        sb.replace(2,5,"world");
        System.out.println(sb);
    }
}
//12truehello34.56
//*********************************
//12worldehello34.56

反转功能

  • public StringBuffer reverse()(如果序列中包含任何替代对,则将它们视为单个字符进行反向操作)
public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("StringBuffer");
        sb.reverse();
        System.out.println(sb);
    }
}
//reffuBgnirtS

截取功能

  • public String substring(int start)
  • public String substring(int start,int end)
    截取功能和前面几个功能的不同:返回值是String类型,本身没有发生改变
public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append(12).append("hello").append(12.34f).append(true);
        System.out.println(sb);
        System.out.println("********************************");

        String sb1 = sb.substring(0, 4);
        System.out.println(sb1);
        System.out.println("********************************");
        System.out.println(sb);
    }
}
//12hello12.34true
//********************************
//12he
//********************************
//12hello12.34true

面试题:String、StringBuffer、StringBuilder类的区别
1、String、StringBuffer、StringBuilder的区别
- String的内容是不可变的,而StringBuffer、StringBuilder的内容是可变的
- StringBuffer是同步线程安全的,数据安全,效率低
- StringBuilder是不同步的,数据不安全,效率高
2、StringBuffer和数组的区别
两个都可以看作是一个容器,装一些数据,但是,StringBuffer最终存储的数据是一个字符串数据,而数组可以存放多宗数据,但是同一个数组里面的元素类型一致
 

感谢观看,我是酷酷的涛。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值