Java学习笔记20(String类应用、StringBuffer类、StringBuilder类)

1.获取指定字符串中大小写和数字的个数:

package demo;

public class StringTest {
    public static void main(String[] args) {
        getCount("IamHandsome666");
    }

    public static void getCount(String str) {
        int upper = 0;
        int lower = 0;
        int digit = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= 65 && c <= 90) {
                upper++;
            } else if (c >= 97 && c <= 122) {
                lower++;
            } else if (c >= 48 && c <= 57) {
                digit++;
            }
        }
        System.out.println(upper);//2
        System.out.println(lower);//9
        System.out.println(digit);//3
    }
}

2.将一个字符串中第一个字母转成大写,其余字母转成小写并打印

package demo;

public class StringTest {
    public static void main(String[] args) {
        String string = toConvert("iAMhaNdSoMe");
        System.out.println(string);
        //输出:Iamhandsome
    }

    public static String toConvert(String str) {
        String first = str.substring(0,1);
        String after = str.substring(1);
        first = first.toUpperCase();
        after = after.toLowerCase();
        return first+after;
    }
}

3.从一个长字符串中找小字符串出现的次数:

package demo;

public class StringTest {
    public static void main(String[] args) {
        System.out.println(getStringCount("Ilikejava,andjavaisthebest,java", "java"));
    }

    public static int getStringCount(String str, String key) {
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(key)) != -1) {
            count++;
            str = str.substring(index+key.length());
        }
        return count;
    }
}
//输出:3

String字符串无法改变,会有一些不便之处

所以介绍一个新类

StringBuffer类,字符串缓冲区

出现目的:为了提高字符串操作效率

内部采用了可变数组的方法,类内部定义了数组,这个数组没有final

数组的默认容量是16

关于它的方法,这里用一个示例来理解:

package demo;

public class StringBufferDemo {
    public static void main(String[] args) {
        append();
        delete();
        insert();
        replace();
        reverse();
        toString_();
    }
    
    public static void append(){
        StringBuffer buffer = new StringBuffer();
        buffer.append(6);
        buffer.append("hello");
        System.out.println(buffer);
        //6hello
    }
    public static void delete(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("helloIlikeJava");
        buffer.delete(1, 2);
        buffer.deleteCharAt(8);
        System.out.println(buffer);
        //hlloIlikJava
    }
    public static void insert(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("java");
        buffer.insert(1, "Python");
        System.out.println(buffer);
        //jPythonava
    }
    public static void replace(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("abcdefg");
        buffer.replace(2, 4, "H");
        System.out.println(buffer);
        //abHefg
    }
    public static void reverse(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("abcdefg");
        buffer.reverse();
        System.out.println(buffer);
        //gfedcba
    }
    public static void toString_(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("abcdefg");
        String string = buffer.toString();
        System.out.println(string);
        //输出一个String类型的abcdefg,即字符串
    }
}

StringBuffer类实例:

public class StringBufferTest {
    public static void main(String[] args) {
        int[] arr = {4,1,4,56,7,8,76};
        System.out.println(toString(arr));
    }
   /*
    * 目的:
    * int[] arr = {34,12,89,68};将一个int[]中元素转成字符串 
    * 格式 [34,12,89,68]
    */
    public static String toString(int[] arr){
        //创建字符串缓冲区
        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        //数组遍历
        for(int i = 0 ; i < arr.length;i++){
            //判断是不是数组的最后一个元素
            if(i == arr.length-1){
                buffer.append(arr[i]).append("]");
            }else{
                buffer.append(arr[i]).append(",");
            }
        }
        return buffer.toString();
    }
}

还有一个StringBuilder类,方法和StringBuffer的方法完全相同

区别:

StringBuffer类是一个线程安全的类,StringBuilder类是一个线程不安全的类,不过它更快

线程知识在后边会讲到,

日常开发建议使用StringBuilder类,因为相对速度更快

原文地址:https://www.cnblogs.com/xuyiqing/p/8259306.html