1. 程式人生 > >JAVA-String與StringBuffer

JAVA-String與StringBuffer

String類

String引用型別,它提供如下方法:

String str = 123;

String str = “aaa”;

方法名

說明

length()

字串長度

charAt()

提取字串中的字元

equals()

比較字串

equalsIgnoreCase()

比較時忽略大小寫

toLowerCase()

轉小寫

toUpperCase()

轉大寫

concat()

連線字串

indexOf()

查詢字串b在a中首次出現的位置,如果查詢不到, 返回-1

lastIndexOf()

查詢字串b在a中最後一次出現的位置

substring()

提取子字串

trim()

去掉字串首尾的空格

split()

分割字串,返回String[]

contains()

判斷字串a中是否包含字串b

valueOf()

將基本型別轉換為字串型

replace()

將字串中的舊字元替換為新的字元

startsWith()

以xxx開頭

endsWith()

以xxx結尾

toCharArray()

轉換成字元陣列

getBytes()

將字串轉換為位元組陣列

String(byte[] data, String[] charset)

將位元組陣列轉換為字串

字串的不變性:

對字串a呼叫以上的方法時, 返回的都是新的字串, 不會對原字串物件a做出改變, 除非給a重新賦值.

StringBuffer

StringBuffer:String增強版

對字串頻繁修改(如字串連線)時,使用StringBuffer類可以大大提高程式執行效率

方法名

說明

append()

末尾追加內容

reverse()

反轉

insert()

插入字元

delete()

刪除字元

toString()

轉換為String形式

StringBuffer是可變的, 只要呼叫了 append等方法, 它的值就改變了.

案例程式碼:

import static org.junit.Assert.*;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import org.junit.Test;

public class StringTest {

    @Test
    public void test() {
        String str = new String("hello");
        System.out.println(str);
        String str2 = "hello";
        String str = "hello";
        int n= str.length();
        System.out.println(str == str2);
        System.out.println(n);
    }
    @Test
    public void test1() {
        String str = "hello";
        String str2 = "heLLo";
        if(str.equalsIgnoreCase(str2)){
           System.out.println("相等的");    
        }else{
           System.out.println("兩個字串不相等");
        }
    }
    
    @Test
    public void test2() {
        String str = "HELL23432o";
        String str2 = "heLLo";
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
    }
    @Test
    public void test3() {
        String str = "123";
        int a = 123;
        String st = str+a;
        System.out.println("*---------------------------------*");
        String st2 = str.concat("123");
        
        System.out.println(st);
        System.out.println(st2);
        System.out.println(str+123);
    }
    @Test
    public void test4() {
        String str = "hello,JQK is JAVA not JAVA";
        String str2 = "JAVA";
        System.out.println(str.indexOf(str2,14));
        System.out.println(str.lastIndexOf(str2));
    }
    @Test
    public void test5() {
        String str = "hello,Jhello,QK is JAVA not JAVA";
        System.out.println(str.substring(5, 9));  [ )
        Math.random(); [0,1)
    }
    @Test
    public void test6() {
        String str = "hello,Jhello,QK is JAVA not JAVA";
        System.out.println(str.substring(5, 9));  [ )
        Math.random(); [0,1)
    }
    @Test
    public void test7() {
        String str = "   he     llo  ";
       System.out.println(str.trim());
    }
    @Test
    public void test8() {
        String str = "春眠不覺曉 處處聞啼鳥 夜來風雨聲 花落是多少";
        /*
        春眠不覺曉
        處處聞啼鳥
        夜來風雨聲
        花落是多少
        */
        String [] arr = str.split(" ");
        String [] arr = {"春眠不覺曉","處處聞啼鳥","夜來風雨聲","花落是多少"};
        for (String st : arr) {
            System.out.println(st+"。");
        }
    }
    @Test
    public void test9() {
        String str = "20180706";
        String str2 = "809";
        if(str.contains(str2)){
           System.out.println("模糊查詢到了");    
        }else{
           System.out.println("沒找到");    
        }
    }
    @Test
    public void test10() {
        int a = 0123;
        System.out.println(String.valueOf(a));
    }
    @Test
    public void test11() {
        String str = "小笨蛋";
        String str2 = "笨蛋";
        System.out.println(str.replace("笨蛋", "**"));
    }
    @Test
    public void test12() {
        String str = "張一凡AAABBBB";
        if(str.startsWith("王") && str.contains("AA") && str.endsWith("B")){
            
        }
        System.out.println(str.startsWith("王"));
        System.out.println(str.endsWith("B"));
    }
    @Test
    public void test13() {
        String str = "hello";
        char [] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.println(ch[i]);
        }
        for (char c : ch) {
            System.out.println(c);
        }
        
    }
    @Test
    public void test14() throws UnsupportedEncodingException {
        String str = "哈嘍你好a";
        byte [] by = str.getBytes("UTF-8");
        System.out.println(Arrays.toString(by));
    }
    @Test
    public void test15() throws UnsupportedEncodingException {
        byte [] by = {-27, -109, -120, -27, -106, -67, -28, -67, -96, -27, -91, -67, 97};
        String st = new String(by,"UTF-8");
        System.out.println(st);
    }
    @Test
    public void test16(){
        long start = System.currentTimeMillis(); //獲取當前時間
        StringBuffer str = new StringBuffer("ch");
        String str = "ch";
        for(int i = 1; i <= 10; i++){
            str.append("aa");
            str += "aa";
        }
        long end = System.currentTimeMillis(); //獲取當前時間
        System.out.println(str);
        String str2 = str.toString();
        System.out.println(str2);
        System.out.println("執行了"+(end-start)+"毫秒");
    }
    @Test
    public void test17(){
        String str = new String("hello");
        StringBuffer strb = new StringBuffer("hello");
        strb.insert(2, "world");
        strb.delete(2, 7);
        System.out.println(strb);
        System.out.println(strb.reverse());
        
    }
}