String、StringBuffer以及StringBuilder的常用方法总结

String类

1、String类的常用方法

1.1 字符串长度

  • public int length():返回字符串长度。

    package Demo05;
    
    public class TestString {
        public static void main(String[] args) {
            String str = "woshiguhuo";
            System.out.println(str.length()); // 10
        }
    }
    

1.2 根据下标获取字符。

  • public char charAt(int index):依据下标获取字符。

    package Demo05;
    
    public class TestString {
    
        public static void main(String[] args) {
            String str = "woshiguhuo";
            char[] strArrays = new char[str.length()];
            for (int i = 0; i < strArrays.length; i++) {
                strArrays[i] = str.charAt(i);
            }
            System.out.println(strArrays); // woshiguhuo
    
        }
    }
    

1.3 判断当前字符串中是否包含str

  • public boolean contains(String str):判断当前字符串中是否包含str。

    package Demo05;
    
    public class TestString {
    
        public static void main(String[] args) {
            String str = "woshiguhuo";
            if (str.contains("guhuo")) {
                System.out.println("guhuo is contained!"); // guhuo is contained!
            } else {
                System.out.println("guhuo is not contained");
            }
        }
    }
    

1.4 将字符串转化为字符串数组

  • public char[] toCharArray():将字符串转化为字符串数组。
package Demo05;

public class TestString {

    public static void main(String[] args) {
        String str = "woshiguhuo";
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            System.out.print(charArray[i]+" ");
            /**
             * w o s h i g u h u o
             * */
        }
    }
}

1.5 查找str首次出现的下标

  • public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1;

    package Demo05;
    
    public class TestString {
    
        public static void main(String[] args) {
            String str = "woshiguhuo";
            System.out.println(str.indexOf("wo"));
            System.out.println(str.indexOf("shi"));
            System.out.println(str.indexOf("guhuo"));
        }
    }
    /**
     * 0
     * 2
     * 5
     * */
    

1.6 查找字符串在当前字符串中最后一次出现的下标索引

  • public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
package Demo05;

public class TestString {

    public static void main(String[] args) {
        String str = "woshiguhuo";
        System.out.println(str.lastIndexOf("wo"));
        System.out.println(str.lastIndexOf("shi"));
        System.out.println(str.lastIndexOf("guhuo"));
    }
}
/**
 * 0
 * 2
 * 5
 * */

1.7 去掉字符串前后的空格

  • public String trim():去掉字符串前后的空格。
package Demo05;

public class TestString {

    public static void main(String[] args) {
        String str = " woshiguhuo ";
        System.out.println(str.trim()); // woshiguhuo
    }
}

1.8 将小写转成大写

  • public String toUpperCase():将小写转成大写.

    package Demo05;
    
    import java.util.Locale;
    
    public class TestString {
    
        public static void main(String[] args) {
            String str = "woshiguhuo";
            String str1 = str.toUpperCase();
            System.out.println(str1); // WOSHIGUHUO
            System.out.println(str1.toLowerCase()); // woshiguhuo
        }
    }
    

1.9 判断字符串是否以str结尾

  • public boolean endsWith(String str):判断字符串是否以str结尾。

    package Demo05;
    
    public class TestString {
    
        public static void main(String[] args) {
            String str = "woshiguhuo";
            if (str.endsWith("guhuo")) {
                System.out.println("str end with guhuo"); // str end with guhuo
            } else {
                System.out.println("str end without guhuo");
            }
        }
    }
    

1.10 将旧字符串替换成新字符串

  • public String replace(char oldChar, char newChar);
package Demo05;

public class TestString {

    public static void main(String[] args) {
        String str = "woshiguhuo";
        String str1 = str.replace("guhuo","mazi");
        System.out.println(str1); // woshimazi
    }
}

1.11、根据str做拆分

  • public String[] split(String str):根据str做拆分。

    package Demo05;
    
    public class TestString {
    
        public static void main(String[] args) {
            String str = "wo shi gu huo";
            String[] chars = str.split(" ");
            for (int i = 0; i < chars.length; i++) {
                System.out.print(chars[i]+"/"); //wo/shi/gu/huo/
            }
        }
    }
    

可变字符串类

StringBuffer和StringBuilder之间的区别:前者线程安全,后者线程不安全;前者效率低,后者效率高。

1. 定义

package Demo05;

public class testStringBuffer {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        StringBuilder stringBuilder = new StringBuilder();
    }
}

2.增

append()

package Demo05;

public class testStringBuffer {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("woshi");
        StringBuilder stringBuilder = new StringBuilder("nishi");
        stringBuffer.append("guhuo");
        stringBuilder.append("mazi");
        System.out.println(stringBuffer); // woshiguhuo
        System.out.println(stringBuilder); // nishimazi
    }
}

insert(int index, str String)

package Demo05;

public class testStringBuffer {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("woshi");
        StringBuilder stringBuilder = new StringBuilder("nishi");
        stringBuffer.append("guhuo");
        stringBuilder.append("mazi");
        stringBuffer.insert(0,"dajiahao!");
        stringBuilder.insert(9,"!");
        System.out.println(stringBuffer); // dajiahao!woshiguhuo
        System.out.println(stringBuilder); // nishimazi!
    }
}

3.删

delet(int startIndx,int endIndex)和deleteCharAt(int Index)

package Demo05;

public class testStringBuffer {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("woshi");
        StringBuilder stringBuilder = new StringBuilder("nishi");
        stringBuffer.append("guhuo");
        stringBuilder.append("mazi");
        stringBuffer.insert(0,"dajiahao!");
        stringBuilder.insert(9,"!");
        stringBuffer.delete(0,5);
        stringBuilder.deleteCharAt(9);
        System.out.println(stringBuffer); // hao!woshiguhuo
        System.out.println(stringBuilder); // nishimazi
    }
}

4.改

replace的endIndex是在其后一位!!!

package Demo05;

public class testStringBuffer {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("woshi");
        StringBuilder stringBuilder = new StringBuilder("nishi");
        stringBuffer.replace(0,2,"ni"); // 注:endIndex是在其后一位!!!
        stringBuilder.replace(0,2,"wo");
        System.out.println(stringBuffer); // nishi
        System.out.println(stringBuilder); // woshi
    }
}

5.转换为字符串

toString()

package Demo05;

public class testStringBuffer {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("woshi");
        StringBuilder stringBuilder = new StringBuilder("nishi");
        System.out.println(stringBuffer.toString().getClass()); // class java.lang.String
        System.out.println(stringBuilder.toString().getClass()); // class java.lang.String
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值