String以及StringBuffer的基本操作

代码总结

package StringStringBuffer.exam;

import java.util.Arrays;

public class test {
    /*equals:比较的是内容,区分大小写*/
    public static void main1(String[] args) {
        String str1 = new String("Hello");
        String str2 = new String("Hello");
        System.out.println(str1 == str2);
        System.out.println(str1.equals(str2));
    }

    /*compareTo:可以区分大小关系,比较规则是字典序*/
    public static void main2(String[] args) {
        System.out.println("A".compareTo("a"));
        System.out.println("a".compareTo("A"));
        System.out.println("A".compareTo("A"));
        System.out.println("AB".compareTo("AC"));
        System.out.println("刘".compareTo("杨"));
    }

    /*toCharArray:将字符串变为字符数组*/
    public static void main3(String[] args) {
        String str = "agfdhgf";
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
    }

    /*charAt:取得指定索引位置的字符,索引从0开始,到length-1*/
    public static void main4(String[] args) {
        String str = "dhshgf";
        char c = str.charAt(0);
        System.out.println(c);
        System.out.println(str.charAt(str.length() - 1));

    }

    /*public String(char value[]):利用这个构造方法可以将字符数组中的内容变为字符串*/
    public static void main5(String[] args) {
        char[] str = {'a', 'b', 'c', 'd'};
        String s = new String(str);
        System.out.println(s);
    }

    /*public String(char value[],int offset,int count):可以将这个字符数组变为字符串*/
    public static void main6(String[] args) {
        char[] str = {'a', 'b', 'c', 'd'};
        String s = new String(str, 0, 1);
        String s1 = new String(str, 1, 2);
        String s2 = new String(str, 1, 4);//会报字符串下标越界
        System.out.println(s);
        System.out.println(s1);
    }

    /*equalsIqnoreCase:不区分大小写的比较*/
    public static void main7(String[] args) {
        String str1 = "abs";
        String str2 = "Abs";
        System.out.println(str1.equals(str2));//false
        System.out.println(str1.equalsIgnoreCase(str2));//true
    }

    /*contains:判断一个子字符串是否存在*/
    public static void main8(String[] args) {
        String str = "dghfgdskfdddfsj";
        boolean ddd = str.contains("ddd");
        System.out.println(ddd);
    }

    /*indexof:查找指定字符串的位置,找到了返回字符串位置的开始索引,找不到返回-1,可以从头开始也可以从指定位置开始*/
    public static void main9(String[] args) {
        String str = "dghfgdskfdddfsj";
        System.out.println(str.indexOf("fsj"));//从头开始查找
        System.out.println(str.indexOf("fsj", 5));//从指定位置开始查找
    }

    /*lastIndexOf:查找指定字符串的位置,找到了返回字符串位置的开始索引,找不到返回-1,可以从后开始查找返回字符串开始索引,也可以从指定位置位置开始查找返回字符串位置的开始索引*/
    public static void main10(String[] args) {
        String str = "dghfgdskfdddfsj";
        System.out.println(str.lastIndexOf("fsj"));
        System.out.println(str.lastIndexOf("fsj", 11));//因为是从后往前找的,所以从这找就找不到
    }

    /*startsWith:判断是否以指定字符串开头,也可以从指定位置判断是否以指定字符串开头*/
    public static void main11(String[] args) {
        String str = "dghfgdskfdddfsj";
        System.out.println(str.startsWith("dg"));
        System.out.println(str.startsWith("ddd", 9));
    }

    /*endsWith:判断是否以指定字符串结尾*/
    public static void main12(String[] args) {
        String str = "dghfgdskfdddfsj";
        System.out.println(str.endsWith("fsj"));
    }

    /*replaceAll/replace:替换所有的指定内容
     * */
    public static void main13(String[] args) {
        String str = "dghfgdskfdddfsj";
        System.out.println(str.replace('d', 'a'));
        System.out.println(str.replace("gd", "王"));
        System.out.println(str.replaceAll("d", "a"));
        System.out.println(str.replaceAll("gd", "王"));
    }

    /*replaceFirst:替换首个内容*/
    public static void main14(String[] args) {
        String str = "dghfgdskfdddfsj";
        System.out.println(str.replaceFirst("g", "d"));
    }

    /*split:将一个完整的字符串按照指定的分隔符划分成若干个子字符串*/
    public static void main15(String[] args) {
        String str = "hello world hello bit";
        String[] s = str.split(" ");//将字符串按空格全部拆分
        System.out.println(Arrays.toString(s));
        String[] s1 = str.split(" ", 2);//将字符串从第一个空格处拆分成两个
        System.out.println(Arrays.toString(s1));
        String str2 = "192*168+1.1'2\\34|543";//这些特殊字符作为分隔符需要加上转义字符\\
        String[] split = str2.split("\\\\");
        System.out.println(Arrays.toString(split));
    }

    /*substring:从一个完整字符串中截取出部分内容*/
    public static void main16(String[] args) {
        String str = "dshfgsdhgfjds";
        System.out.println(str.substring(1));//从指定索引截取到结尾
        System.out.println(str.substring(1, 3));//左闭右开,截取部分内容
    }

    /*trim():去掉字符串两边的左右空格,但是中间的空格,不去除*/
    public static void main17(String[] args) {
        String str = "  he  ld  ";
        System.out.println(str.trim());
    }

    /*toUpperCase()/toLowerCase():字符串中所有字母转大写,字符串中所有字母转小写*/
    public static void main18(String[] args) {
        String str = "gdfh FDHGkjGJ342434vhjkfd";
        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
    }

    /*intern:字符串入池操作*/
    public static void main19(String[] args) {
        String str1 = new String("hello").intern();
        String str2 = "hello";
        System.out.println(str1 == str2);
    }

    /*concat:字符串连接,等同于“+”,但是不入池*/
    public static void main20(String[] args) {
        String str1 = "he";
        String str2 = "llo";
        String str = str1 + str2;//形成的hello,但是没入池
        String str4 = "hello";
        System.out.println(str);
        String str3 = str1.concat(str2);//没入池
        System.out.println(str3);
        System.out.println(str == str4);
        System.out.println(str == str3);
    }

    /*length:取得字符串的长度*/
    public static void main21(String[] args) {
        String str = "dshfhdsfg";
        int length = str.length();
        System.out.println(length);
    }

    /*isEmpty():判断是否为空字符串,但不是null,而是null,而是长度为0*/
    public static void main22(String[] args) {
        String str = "";
        String str1 = null;
        System.out.println(str.isEmpty());
        System.out.println(str.length());
        System.out.println(str1.isEmpty());//会报空指针异常
    }

    /*================================================================================================*/
    /*由于String的不可更改性,为了方便字符串的修改,提供StringBuffer和StringBuilder类*/
    /*String和StringBuffer的互换*/
    public static void main23(String[] args) {
        String str = "hdhgfs";
        StringBuffer stringBuffer = new StringBuffer(str);//利用StringBuffer的构造方法将String转换为StringBuffer
        StringBuffer stringBuffer1 = new StringBuffer();
        StringBuffer append = stringBuffer1.append(str);//利用append将String转化为String
        String s = stringBuffer.toString();//利用toString将StringBuffer转化为String
    }

    /*reverse:字符串反转*/
    public static void main24(String[] args) {
        StringBuffer sb = new StringBuffer("helloworld");
        sb.reverse();
        System.out.println(sb);
    }

    /*delete:删除指定范围的数据*/
    public static void main25(String[] args) {
        StringBuffer sb = new StringBuffer("helloworld");
        sb.delete(5, 9);//左闭右开
        System.out.println(sb);
    }

    /*insert:插入数据*/
    public static void main26(String[] args) {
        StringBuffer sb = new StringBuffer("helloworld");
        sb.insert(1, "王根生");
        System.out.println(sb);
    }

    /*append:字符串连接*/
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("helloworld");
        StringBuffer sb1 = new StringBuffer("helloworld234");

        sb.append("王根生");
        sb.append(sb1);
        System.out.println(sb);

    }
    /*StringBuffer线程安全,StringBuilder线程不安全*/
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值